@objc协议崩溃了swift编译器

我写了我的协议,这是打算有一些@optional方法,但swift编译器崩溃。 这工作:

 protocol SessionDelegate { // TODO these should all be optional func willOpenSession(session: Session); func didOpenSession(session: Session); func didFailOpenningSession(session: Session, error: NSError!); func willCloseSession(session: Session); func didCloseSession(session: Session); } 

这不:

 @objc protocol SessionDelegate { @optional func willOpenSession(session: Session); @optional func didOpenSession(session: Session); @optional func didFailOpenningSession(session: Session, error: NSError!); @optional func willCloseSession(session: Session); @optional func didCloseSession(session: Session); } 

老实说,拥有@objc足以使编译器崩溃。 有没有解决办法?

现在你唯一的解决方法是在Objective-C头文件中声明协议,并通过Objective-C桥接头来导入声明。

议定书声明:

 // SessionDelegate.h @class Session; @protocol SessionDelegate <NSObject> @optional - (void)willOpenSession:(Session *)session; - (void)didOpenSession:(Session *)session; - (void)didFailOpenningSession:(Session *)session error:(NSError *)error; - (void)willCloseSession:(Session *)session; - (void)didCloseSession:(Session *)session; @end 

桥接标题:

 // MyProject-Bridging-Header.h #import "SessionDelegate.h" 

在Swift中遵循类实现:

 // Session.swift class Session { // ... } class MySessionDelegate: NSObject, SessionDelegate { func willOpenSession(session: Session) { // ... } } 

道歉,抓我以前的编辑,尝试下面的代替:

 @objc(PSessionDelegate) protocol PSessionDelegate { @optional func willOpenSession(session: Session); @optional func didOpenSession(session: Session); @optional func didFailOpenningSession(session: Session, error: NSError!); @optional func willCloseSession(session: Session); @optional func didCloseSession(session: Session); } class ViewController: UIViewController, PSessionDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }