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

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.

主站蜘蛛池模板: 寻乌县| 阿克陶县| 乃东县| 长武县| 商都县| 吴江市| 芦山县| 大理市| 集安市| 徐汇区| 赤城县| 萨嘎县| 青州市| 大石桥市| 山西省| 黄陵县| 鱼台县| 金华市| 华宁县| 高密市| 黎川县| 潢川县| 且末县| 漾濞| 冀州市| 镇安县| 迁安市| 威宁| 四会市| 郯城县| 张家口市| 四会市| 赞皇县| 易门县| 鄂伦春自治旗| 慈利县| 乃东县| 剑阁县| 且末县| 新乡县| 东阳市|