Swift代理报错Optional can only be applied to members of an @objc protocol

737 阅读1分钟

报错信息

Swift是不允许设置协议的可选的状态,协议声明的方法都是必须实现的。

解决方案一

@objc
添加@objc修饰协议。

解决方案二

实现所有声明的方法

我的做法

在父类中实现声明的方法

    class HLBaseViewController: UIViewController, UIViewCollectEventsDelegate

    func uiView(ClickRefreshWithParams params: AnyObject?) {
        
    }
    
    func uiView(uiView: UIView?, CollectEventsType type: AnyObject?, withParams params: AnyObject?) {
        
    }

在子类中替换掉要实现的方法

    class HomeViewController: HLBaseViewController 
 

    override func uiView(uiView: UIView?, CollectEventsType type: AnyObject?, withParams params: AnyObject?) {
        
        NSLog("789")

    }