协议和扩展

使用 protocol 来声明一个协议。

protocolExampleProtocol { var simpleDescription: String { get } mutatingfuncadjust() }

类、枚举和结构体都可以遵循协议。

classSimpleClass:ExampleProtocol { var simpleDescription: String="A very simple class." var anotherProperty: Int=69105 funcadjust() { simpleDescription +=" Now 100% adjusted." } } var a =SimpleClass() a.adjust() let aDescription = a.simpleDescription structSimpleStructure:ExampleProtocol { var simpleDescription: String="A simple structure" mutatingfuncadjust() { simpleDescription +=" (adjusted)" } } var b =SimpleStructure() b.adjust() let bDescription = b.simpleDescription

练习

ExampleProtocol 再增加一个要求。你需要怎么改 SimpleClassSimpleStructure 才能保证它们仍旧遵循这个协议?

注意声明 SimpleStructure 时候 mutating 关键字用来标记一个会修改结构体的方法。SimpleClass 的声明不需要标记任何方法,因为类中的方法通常可以修改类属性(类的性质)。

使用 extension 来为现有的类型添加功能,比如新的方法和计算属性。你可以使用扩展让某个在别处声明的类型来遵守某个协议,这同样适用于从外部库或者框架引入的类型。

extensionInt:ExampleProtocol { var simpleDescription: String { return"The number \(self)" } mutatingfuncadjust() { self +=42 } } print(7.simpleDescription)

练习

Double 类型写一个扩展,添加 absoluteValue 属性。

你可以像使用其他命名类型一样使用协议名——例如,创建一个有不同类型但是都实现一个协议的对象集合。当你处理类型是协议的值时,协议外定义的方法不可用。

let protocolValue: ExampleProtocol = a print(protocolValue.simpleDescription) // print(protocolValue.anotherProperty) // 去掉注释可以看到错误

即使 protocolValue 变量运行时的类型是 simpleClass ,编译器还是会把它的类型当做 ExampleProtocol。这表示你不能调用在协议之外的方法或者属性。