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

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.

主站蜘蛛池模板: 含山县| 赤城县| 合阳县| 广西| 万山特区| 吉木乃县| 基隆市| 双鸭山市| 乌兰察布市| 桐庐县| 耿马| 西乡县| 和平县| 盘锦市| 东兰县| 专栏| 菏泽市| 海原县| 永新县| 阜阳市| 历史| 广丰县| 环江| 界首市| 贵州省| 含山县| 汤阴县| 开阳县| 雅江县| 怀安县| 宁安市| 澳门| 威远县| 新疆| 溆浦县| 绥江县| 桑植县| 丹巴县| 新丰县| 汝州市| 宝山区|