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

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.

主站蜘蛛池模板: 江孜县| 陇西县| 湟中县| 泊头市| 甘谷县| 江津市| 黑水县| 绿春县| 博野县| 咸丰县| 通城县| 阜宁县| 乐山市| 镇赉县| 焉耆| 方城县| 铁力市| 长海县| 开原市| 孝昌县| 龙里县| 柘城县| 松桃| 玉田县| 开化县| 台州市| 万山特区| 慈利县| 永仁县| 东山县| 徐汇区| 西藏| 广饶县| 丰顺县| 抚宁县| 灌南县| 禄丰县| 丽水市| 五莲县| 博爱县| 陆河县|