2016-05-10 2 views
3

Lassen Sie uns dieses Beispiel betrachten, die ohne Probleme kompiliert:Conformance Fragen auch nach Anforderungen mit Protokollerweiterungen für @objc Protokolle erfüllen

protocol AProtocol { 
    func veryImportantMethod() 
} 

protocol BProtocol : AProtocol { 

} 

extension BProtocol { 
    func veryImportantMethod() { 
     print("A's protocol requirement satisfied!") 
    } 
} 

class ConcreteClass : BProtocol { 

} 

jedoch, wenn wir dies tun:

@objc protocol AProtocol { //Added @objc here 
    func veryImportantMethod() 
} 

protocol BProtocol : AProtocol { 

} 

extension BProtocol { 
    func veryImportantMethod() { 
     print("A's protocol requirement satisfied!") 
    } 
} 

class ConcreteClass : BProtocol { 

} 

Es spielt keine 't Kompilieren mit einer Nachricht:

Typ' ConcreteClass 'entspricht nicht dem Protokoll' AProtocol '

Aber wenn wir es tatsächlich umsetzen,

class ConcreteClass : NSObject, BProtocol { 
    func veryImportantMethod() { } 
} 

der Code kompiliert wird. Warum passiert dies? Gibt es etwas, dass ich hier vermisse?

Ich versuche, diese Form der Protokollhierarchie für UITableViewDataSource zu erreichen, und ich möchte wirklich tableView:cellForRowAtIndexPath: Code über die Konformitäten nicht wiederholen.

Antwort

0

Der Compiler möchte sicherstellen, dass die Klasse mit ObjC kompiliert werden kann. Sobald Sie von einer ObjC-Klasse erben (wie NSObject), weiß der Compiler das, aber Sie implementieren die Erweiterung vor diesem Punkt. Sie können den Fehler vermeiden, indem Sie das Protokoll definieren, das von einer Klasse implementiert werden soll, die von NSObject wie folgt lautet:

@objc protocol AProtocol { 
    func veryImportantMethod() 
} 

protocol BProtocol : AProtocol { 

} 

class BProtocolClass: NSObject { 

} 

extension BProtocolClass { 
    func veryImportantMethod() { 
     print("A's protocol requirement satisfied!") 
    } 
} 

class ConcreteClass: BProtocolClass { 

} 
Verwandte Themen