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

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.

主站蜘蛛池模板: 巴彦淖尔市| 洛扎县| 喀喇| 巴里| 皮山县| 小金县| 东阳市| 上思县| 清徐县| 卫辉市| 渝中区| 大宁县| 惠州市| 青河县| 望江县| 万源市| 西青区| 富平县| 印江| 房产| 丰镇市| 抚远县| 庆安县| 延庆县| 达日县| 纳雍县| 桂平市| 武平县| 武平县| 苏尼特左旗| 顺平县| 庄河市| 井研县| 仙桃市| 东乌珠穆沁旗| 津南区| 安康市| 浦北县| 喀喇沁旗| 冕宁县| 朝阳市|