数组元素无法桥接到 Objective-C [英] Array element cannot be bridged to Objective-C
问题描述
我有这段代码可以创建一个视图并对其应用渐变.
I have this code that creates a view and applies a gradient to it.
import UIKit
import QuartzCore
let rect : CGRect = CGRectMake(0,0,320,100)
var vista : UIView = UIView(frame: rect)
let gradient : CAGradientLayer = CAGradientLayer()
gradient.frame = vista.bounds
let cor1 = UIColor.blackColor()
let cor2 = UIColor.whiteColor()
let arrayColors = [cor1.CGColor, cor2.CGColor]
gradient.colors = arrayColors
view.layer.insertSublayer(gradient, atIndex:0)
Xcode 没有给我任何编译错误,但是代码在线上崩溃了
Xcode is giving me no compile error, but the code is crashing on the line
let arrayColors = [cor1.CGColor, cor2.CGColor]
带有消息数组元素无法桥接到Objective-C
事实上,我原以为它会在那里崩溃,因为我不确定如何在 Swift 上创建一组 CGColors.令人惊讶的是 Xcode 提到了 Objective-C.在我看来,我正在迅速创建一个 CGColorRef...
In fact I was expecting it to crash there, because I am not sure how I can create an array of CGColors on Swift. The surprise here is Xcode mentioning Objective-C. In my mind I was creating a CGColorRef in swift...
有什么线索吗?为什么要提到 Objective-C,我该如何解决?
Any clues? Why is it mentioning Objective-C and how do I solve this?
推荐答案
之所以提到 Objective-C,是因为 UIKit 和 QuartzCore 是 Objective-C 框架.特别是,gradient.colors = arrayColors
正在调用一个需要 NSArray
的 Objective-C 方法.
The reason Objective-C is mentioned is because UIKit and QuartzCore are Objective-C frameworks. In particular, gradient.colors = arrayColors
is calling an Objective-C method that expects an NSArray
.
这似乎是一个错误,因为 Apple 的文档听起来像是数组应该自动桥接到 NSArray
,只要数组中的项目可以被视为 AnyObject代码>:
This seems like a bug, as Apple's documentation makes it sound like that the array should auto-bridge to an NSArray
so long as the items in the array can be considered AnyObject
:
当你从一个 Swift 数组桥接到一个 NSArray 对象时,元素在 Swift 数组中必须与 AnyObject 兼容.例如,一个 SwiftInt[] 类型的数组包含 Int 结构元素.Int 类型是不是类的实例,而是因为 Int 类型桥接到NSNumber 类,Int 类型与 AnyObject 兼容.因此,你可以将 Int[] 类型的 Swift 数组桥接到 NSArray 对象.如果Swift 数组中的元素与 AnyObject 不兼容,这是一个运行时错误当您桥接到 NSArray 对象时发生.
When you bridge from a Swift array to an NSArray object, the elements in the Swift array must be AnyObject compatible. For example, a Swift array of type Int[] contains Int structure elements. The Int type is not an instance of a class, but because the Int type bridges to the NSNumber class, the Int type is AnyObject compatible. Therefore, you can bridge a Swift array of type Int[] to an NSArray object. If an element in a Swift array is not AnyObject compatible, a runtime error occurs when you bridge to an NSArray object.
您也可以按照上述相同的桥接规则,直接从 Swift 数组字面量创建 NSArray 对象.当你显式键入一个常量或变量作为 NSArray 对象并赋值它是一个数组文字,Swift 创建一个 NSArray 对象而不是一个Swift 数组.
You can also create an NSArray object directly from a Swift array literal, following the same bridging rules outlined above. When you explicitly type a constant or variable as an NSArray object and assign it an array literal, Swift creates an NSArray object instead of a Swift array.
目前,解决方法是将 arrayColors
声明为 NSArray
:
For now, a work around would be either to declare arrayColors
as an NSArray
:
let arrayColors: NSArray = [cor1.CGColor, cor2.CGColor]
或者将其声明为采用 AnyObject
:
Or to declare it as taking AnyObject
:
let arrayColors: Array
这篇关于数组元素无法桥接到 Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!