は、このコードを取る:A.selfの使用を避けるには?
protocol P: class {
static var hello: String { get }
}
class A: P {
class var hello: String {
return "Hello"
}
}
class B: A {
override static var hello: String {
return "Hello World"
}
}
class C: A {}
class D: C {
override static var hello: String {
return "Hello D"
}
}
func sayHello(elements: P.Type...) {
for p in elements {
print(p.hello)
}
}
func sayHelloAgain(elements: A.Type...) {
for p in elements {
print(p.hello)
}
}
func sayHelloThe3rd(elements: [A.Type]) {
for p in elements {
print(p.hello)
}
}
sayHello(A.self, B.self, C.self)
sayHelloAgain(A.self, B.self, C.self)
は、私は一つのケースではなく、他にA.selfを使用しなければならないのはなぜ
func register<T: UITableViewCell where T: ReusableView, T: NibLoadableView>(_: T.Type) { ... }
tableView.register(FoodTableViewCell)
(このpresentationから取られた)このにそれを比較しますか? また、引数を1つ指定して呼び出すときに.selfを使用する必要はありません。
sayHello(A)
sayHello(A, B) //doesn't compile
あなたが参照している古いコードは、 '.self'を追加するべきであるという警告を生成します。以前のバージョンのSwiftでは '.self'が推測され、コンパイラがそれをスティックするケースがありましたが、SwiftはAlexanderが以下のように常に要求するように動いています。 –