swift - 类型不可转换为 'NativeObject'

标签 swift xcode6

自从我将我的 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/

相关文章:

swift - 使 TableViewCells 从 Storyboard Prototype 单元格中出列?

xcode - 通过 Xcode 版本控制中的文件提交消息

ios - 如何禁用尺寸类别的一项特定约束?

ios - 如何使用 NSLog 调试 iOS 8 扩展?

swift - 无法构建 objective-c 模块 'FBSDKCoreKit'

ios - 为我的变量编写自定义 Setter - Swift

ios - 在将数组发送到 API 端点之前序列化数组

ios - 如何过滤附件 View 中的 Collection View 的数组列表

ios - Swift 1.2 崩溃与 .lowercaseString

swift - 切换到 Xcode 6 Beta 5 后,所有 IBOutlets 都变为 nil