- Swift 4 Protocol-Oriented Programming(Third Edition)
- Jon Hoffman
- 128字
- 2021-07-08 09:42:35
Optional requirements
There are times when we want protocols to define optional requirements. An optional requirement is a method or property that is not required to be implemented. To use optional requirements, we need to start off by marking the protocol with the @objc attribute.
It is important to note that only classes can adopt protocols that use the @objc attribute. Structures and enumerations cannot adopt these protocols.
To mark a property or method as optional, we use the optional keyword. The following example shows how we would create both an optional property and also an optional method:
@objc protocol Phone { var phoneNumber: String {get set} @objc optional var emailAddress: String {get set} func dialNumber() @objc optional func getEmail() }
Now let's explore how protocol inheritance works.
推薦閱讀
- 華為HMS生態與應用開發實戰
- Responsive Web Design with HTML5 and CSS3
- Object-Oriented JavaScript(Second Edition)
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- 單片機應用與調試項目教程(C語言版)
- Go語言底層原理剖析
- Solutions Architect's Handbook
- Hadoop大數據分析技術
- IoT Projects with Bluetooth Low Energy
- Learning Nessus for Penetration Testing
- Mastering OpenStack
- Visual C++程序設計全程指南
- 微信公眾平臺服務號開發:揭秘九大高級接口
- Learning RxJava
- 嵌入式網絡編程