官术网_书友最值得收藏!

Optional requirements

You see that when you list your properties and methods in a protocol, the type that conforms to that protocol should adopt to all properties and methods. Skipping one of them will lead to a compiler error. Some protocols may contain methods or properties that are not necessary to implement, especially with delegates. Some delegate methods are meant to notify you something that you don't care about. In that case, you can mark these methods as optional. The keyword optional can be added before properties and methods to mark them as optional. Another thing, the protocol that has optional stuff should be marked with @Objc. Take a look at the following example:

@objc protocol DownloadManagerDelegate { 
   func didDownloadFile(fileURL: String, fileData: NSData) 
   optional func didFailToDownloadFile(fileURL: String, error: NSError) 
} 

It's the new version of DownloadManagerDelegate, which marks didFailToDownloadFile method as optional.

主站蜘蛛池模板: 湖口县| 江城| 平湖市| 扎兰屯市| 贵溪市| 霍林郭勒市| 郴州市| 丰县| 高碑店市| 碌曲县| 筠连县| 寿阳县| 宝清县| 芜湖市| 榆树市| 珲春市| 吴桥县| 定远县| 渝中区| 房山区| 新沂市| 清原| 巫山县| 长宁区| 平度市| 汤阴县| 苗栗县| 筠连县| 图们市| 临猗县| 湖口县| 彰武县| 扎兰屯市| 海城市| 佛冈县| 余干县| 荔波县| 理塘县| 深泽县| 天祝| 湘潭市|