objective-c - 'void *' 类型的集合元素不是 Objective-C 对象

标签 objective-c xcode ios8

我正在尝试将渐变 View 添加到 iOS 应用程序的背景中。

使用 pod UIColor+uiGradients 我在 viewDidLoad 下的背景中添加了一个 GradientLayer:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIColor *startColor = [UIColor uig_emeraldWaterStartColor];
    UIColor *endColor = [UIColor uig_emeraldWaterEndColor];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(self.view.frame.size.width, 0);
    gradient.colors = @[(id)[startColor CGColor], (id)[endColor CGColor], nil];

    [self.view.layer insertSublayer:gradient atIndex:0];   
}

错误发生在行 gradients.colors = @[(id)[startColor CGColor], (id)[endColor CGColor], nil];

错误:

Collection element of type 'void *' is not an Objective-C object.

感谢您的帮助。

最佳答案

删除数组末尾的 nil。您不能将 nil 放入 Objective-C 数组中,在 @[] 文字中也没有必要。所以:

gradient.colors = @[(id)[startColor CGColor], (id)[endColor CGColor]];

关于objective-c - 'void *' 类型的集合元素不是 Objective-C 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900146/

相关文章:

ios - 如何识别一个数字是 UPC 码还是简单的数字?是否有任何特定格式来识别 UPC 代码?

ios - 如何将度数转换为弧度?

ios - SKScene:场景的界限

objective-c - 全局变量/单集?

ios - 我怎样才能制作表情符号或输入任何内容

ios - 退出我的应用程序后未显示蓝色横幅 "Your app is using your location"

objective-c - 如何在 Objective-C 中不重复代码? (又名多重继承)

xcode - 为什么在这个构建脚本中是 "xcodebuild: command not found"?

ios - 无法在 iOS 8 模拟器中使用 HTTP

swift - 不正确的 UITableViewCell 高度 UIImageView