- 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.
推薦閱讀
- C++ Primer習(xí)題集(第5版)
- Visual C++串口通信開發(fā)入門與編程實(shí)踐
- iOS 9 Game Development Essentials
- Arduino開發(fā)實(shí)戰(zhàn)指南:LabVIEW卷
- DevOps入門與實(shí)踐
- PyTorch自然語言處理入門與實(shí)戰(zhàn)
- Production Ready OpenStack:Recipes for Successful Environments
- JavaScript+Vue+React全程實(shí)例
- Mastering macOS Programming
- Android底層接口與驅(qū)動(dòng)開發(fā)技術(shù)詳解
- C++反匯編與逆向分析技術(shù)揭秘(第2版)
- 案例式C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- Beginning C++ Game Programming
- Managing Microsoft Hybrid Clouds
- FFmpeg開發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到短視頻上線