Ich möchte eine Methode implementieren, die nur Klassen unterstützte Protokolle nahm. Zu diesem Zweck habe ich ein Protokoll definiert:Swift: Limit-Klassen nur unterstützt Protokoll
protocol TestProt {
func testMe() -> String
}
Lassen Sie uns eine Klassendeklaration tun:
class TestClass: TestProt {
func testMe() -> String {
return "test"
}
}
Aber, wie ich Register Klasse implementieren kann, um nur Klassen unterstützt TestProt?
class FabricaClass {
// it is func take any classes :(
func registerMe(context: MyContext, class: AnyClass) -> String {
}
// i want something like this =)
// to the method took only classes supported protocols
// but it don't work for swift...
func registerMe(context: MyContext, class: AnyClass <TestProt>) -> String {
}
}
UPD: Antwort
protocol TestProt {
static func testMe() -> String
}
class TestClass: TestProt {
class func testMe() -> String {
return "test"
}
}
class MyContext {
}
class FactoryClass {
init<T: TestProt>(context: MyContext, regClass: T.Type) {
regClass.testMe()
}
}
let context = MyContext()
let factory = FactoryClass(context: context, regClass: TestClass.self)
Dank für @dfri
Worth suchen http://stackoverflow.com/a/24051850/2710486 – zcui93