自从我将我的 Xcode 更新到版本 6 beta 3 后,我到处都收到 Type is not convertible to 'NativeObject'
错误。
这只是它提示的一个案例:
func createPointsToPath(points:[AnyObject]!) -> CGPathRef {
let path:CGMutablePathRef = CGPathCreateMutable()
CFAutorelease(path)
if(points != nil && points.count > 0){ //this line complains about '[AnyObject]' is not convertible to 'NativeObject'
var point:CGPoint = (points[0] as NSValue).CGPointValue()
CGPathMoveToPoint(path, nil, point.x, point.y)
println("number of elements in the array \(points.count)")
for index in 1...points.count - 1 {
point = (points[index] as NSValue).CGPointValue()
CGPathAddLineToPoint(path, nil, point.x, point.y)
}
}
return path
}
如代码注释中所述,它提示 '[AnyObject]' is not convertible to 'NativeObject'
我检查 points != nil
最佳答案
更新
从 Beta 5 开始,可选值不再被视为 bool 类型,您必须明确地与 nil 进行比较。这个答案不再有效,但为了后代,我把它留在了下面。当然,由于 Swift 编译器的奥秘,您的原始代码现在可以使用新的更新。
旧的无效答案:
你可以说:
if points && points.count > 0
nil
使用可选值计算为 false,所以这应该可以工作。
关于swift - 类型不可转换为 'NativeObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24703490/