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

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.

主站蜘蛛池模板: 淮滨县| 施甸县| 成安县| 南投县| 甘肃省| 磐石市| 金沙县| 塔河县| 永兴县| 金平| 平顶山市| 建湖县| 桂平市| 绥德县| 海林市| 化德县| 凤台县| 濮阳县| 永善县| 酉阳| 湖口县| 罗源县| 昭苏县| 烟台市| 吉木乃县| 根河市| 房产| 常州市| 易门县| 吉隆县| 石城县| 巴彦淖尔市| 讷河市| 马关县| 盐亭县| 阿合奇县| 三河市| 崇信县| 泰州市| 吴忠市| 怀远县|