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

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.

主站蜘蛛池模板: 翼城县| 吉木乃县| 张家港市| 克拉玛依市| 城口县| 阆中市| 当涂县| 鹿泉市| 政和县| 富民县| 金坛市| 芦山县| 叶城县| 江山市| 瓮安县| 新乐市| 方山县| 辉县市| 肇庆市| 蓬安县| 富顺县| 城步| 平南县| 三江| 房产| 镇坪县| 天峨县| 蒙山县| 郓城县| 阳新县| 麻城市| 赣榆县| 轮台县| 宿松县| 张家川| 东源县| 大田县| 班戈县| 宁乡县| 中西区| 阿鲁科尔沁旗|