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

Type casting with protocols

Type casting is a way to check the type of an instance and/or to treat the instance as a specified type. In Swift, we use the is keyword to check whether an instance is of a specific type and the as keyword to treat an instance as a specific type.

The following example shows how we would use the is keyword:

if person is SwiftProgrammer {  
  print("(person.firstName) is a Swift Programmer") 
} 

In this example, the conditional statement returns true if the Person instance is of the SwiftProgrammer type or false if it isn't. We can use the where statement in combination with the is keyword to filter an array to only return instances of a specific type. In the next example, we filter an array that contains instances of the Person protocol and have it only return those elements of the array that are instances of the SwiftProgrammer type:

for person in people where person is SwiftProgrammer { 
  print("(person.firstName) is a Swift Programmer") 
} 

Now let's look at how we would cast an instance to a specific type. To do this, we can use the as keyword. Since the cast can fail if the instance is not of the specified type, the as keyword comes in two forms: as? and as!. With the as? form, if the casting fails it returns a nil. With the as! form, if the casting fails a runtime error is thrown; therefore, it is recommended to use the as? form unless we are absolutely sure of the instance type or we perform a check of the instance type prior to doing the cast.

The following example shows how we would use the as? keyword to attempt to cast an instance of a variable to the SwiftProgammer type:

if let _ = person as? SwiftProgrammer {  
  print("(person.firstName) is a Swift Programmer") 
} 

Since the as? keyword returns an optional, in the last example we could use optional binding to perform the cast.

Now let's see how we can use associated types with protocols.

主站蜘蛛池模板: 保康县| 湾仔区| 丰都县| 汶川县| 封丘县| 台中市| 长岛县| 扎兰屯市| 亚东县| 锡林郭勒盟| 女性| 普兰店市| 泌阳县| 辰溪县| 永善县| 嘉义市| 灵台县| 五寨县| 靖安县| 什邡市| 荥经县| 喜德县| 靖边县| 石嘴山市| 房山区| 兴海县| 江陵县| 遂溪县| 凭祥市| 饶平县| 梅州市| 奉新县| 海宁市| 莫力| 吴旗县| 台江县| 定陶县| 聂荣县| 惠东县| 五大连池市| 平定县|