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

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.

主站蜘蛛池模板: 阆中市| 砚山县| 汉中市| 静海县| 神木县| 上虞市| 荣昌县| 东乌珠穆沁旗| 梅河口市| 博爱县| 佛教| 佛冈县| 平泉县| 武宣县| 阿克| 双流县| 兴安盟| 襄城县| 长寿区| 固原市| 呼玛县| 海口市| 永州市| 岳普湖县| 德钦县| 寻甸| 深泽县| 布尔津县| 若羌县| 什邡市| 哈巴河县| 临夏县| 顺义区| 宜兰县| 克拉玛依市| 历史| 高邑县| 高尔夫| 正宁县| 岫岩| 广东省|