据我了解,如果需要的话,Array
in Swift将自动转换为NSArray
。
当您从NSArray对象桥接到Swift数组时,结果数组的类型为AnyObject []。如果对象是Objective-C或Swift类的实例,或者该对象可以桥接到一个对象,则该对象是AnyObject兼容的。您可以将任何NSArray对象桥接到Swift数组,因为所有Objective-C对象都是AnyObject兼容的。由于所有NSArray对象都可以桥接到Swift数组,因此Swift编译器在导入Objective-C API时将NSArray类替换为AnyObject []。
Auszug aus:Apple Inc.“将Swift与Cocoa和Objective-C结合使用。” iBooks。https://itunes.apple.com/de/book/using-swift-cocoa-objective/id888894773?mt=11
在我的游乐场中,以下操作失败
[位置]无法转换为“ NSArray”
import Foundation
enum Position : String {
case TopLeft = "Top Left", TopCenter = "Top Center", TopRight = "Top Right", BottomLeft = "Bottom Left", BottomCenter = "Bottom Center", BottomRight = "Bottom Right"
// pattern to iterate enums by http://www.swift-studies.com/blog/2014/6/10/enumerating-enums-in-swift
static let allValues : [Position] = [TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight]
}
let a : NSArray = Position.allValues
据我所知,Position.allValues是有效的Array,尽管是静态的。我想念什么还是有人可以帮忙吗?谢谢。
问题是那allValues
是Position的数组。但是Position是一个枚举,Swift枚举无法越过通往Objective-C的桥梁。
因此,是的,[Int]
或者[String]
将神奇地过桥而成为NSArray,但不是[Position]
-错误消息告诉您的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句