Swift继承了Objective-C的元类概念:类本身也被认为是对象.类Foo的对象的类是Foo.self,它是类型的Foo.Type.如果Foo继承自Bar,那么Foo.self也可以分配给类型的变量Bar.Type.这至少有两个好处:
我现在特别关注第二个.只是为了确保每个人都理解我所追求的,这是一个例子:
class BaseFoo {
var description: String { return "BaseFoo" }
}
class DerivedFoo: BaseFoo {
override var description: String { return "DerivedFoo" }
}
let fooTypes: [BaseFoo.Type] = [BaseFoo.self, DerivedFoo.self] // metaclass magic!
for type in fooTypes {
let object: BaseFoo = type() // metaclass magic!
println(object)
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个AnyClass对象数组(可以分配任何元类实例AnyClass,就像任何对象都可以分配AnyObject),我想找到哪些实现给定的协议.该协议将声明一个初始化器,我将像上面的例子中那样实例化该类.例如:
protocol Foo {
init(foo: String)
}
class Bar: Foo {
required init(foo: String) { println("Bar initialized with \(foo)") }
}
class Baz {
required init() { println("I'm not a Foo!") }
}
let types: [AnyClass] = [Bar.self, Baz.self]
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在,问题在于确定类是否实现了协议.由于元类实例是多态的,我希望能够转换它们.但是,我显然错过了一些东西,因为Swift不会让我这样写:
for type in types {
if let fooType = type as? Foo.Type {
let obj = fooType(foo: "special snowflake string")
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的编译器错误是:
错误:'Foo'与'AnyObject'不同
有没有办法确定元类实例是否表示实现协议的类,有没有办法将该实例强制转换为协议类型?
我试图将其声明Foo为类协议,但显然还不够.
编辑:我只是尝试了Any类型,虽然它不会导致语法错误,但它会崩溃Swift编译器.
从Xcode 7 beta 2和Swift 2开始,它已得到修复。您现在可以编写:
for type in types {
if let fooType = type as? Foo.Type {
// in Swift 2 you have to explicitly call the initializer of metatypes
let obj = fooType.init(foo: "special snowflake string")
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想type输入Foo.Type文字,可以使用for case
for case let type as Foo.Type in types {
let obj = type.init(foo: "special snowflake string")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |