5
Di seguito è riportato il problema, che non è possibile richiamare i metodi di classe se il tipo è protocol.type. C'è un modello alternativo che potrebbe raggiungere questo obiettivo.L'accesso ai membri del tipo di protocollo non funziona in Swift
scenario reale è che ho arr: [SomeProtocol.Type] e voglio eseguire stringhe Let: [String] = arr.map {$ 0.str}
protocol SomeProtocol: class {
static var str: String { get }
static func value() -> Int
}
class SomeClass: SomeProtocol {
static var str: String = "SomeClass"
static func value() -> Int {
return 1
}
}
let protocolType: SomeProtocol.Type = SomeClass.self
println(protocolType.str) // compile error
println(protocolType.value()) // compile error
related: [Dichiara matrice di classi conformi a un protocollo] (http://stackoverflow.com/a/28691376/3804019) – rintaro