使用protocol来声明一个接口。
- protocol ExampleProtocol {
- var simpleDescription: String { get }
- mutating func adjust()
- }
类、枚举和结构体都可以实现接口。
- class SimpleClass: ExampleProtocol {
- var simpleDescription: String = "A very simple class."
- var anotherProperty: Int = 69105
- func adjust() {
- simpleDescription += " Now 100% adjusted."
- }
- }
- var a = SimpleClass()
- a.adjust()
- let aDescription = a.simpleDescription
-
- struct SimpleStructure: ExampleProtocol {
- var simpleDescription: String = "A simple structure"
- mutating func adjust() {
- simpleDescription += " (adjusted)"
- }
- }
- var b = SimpleStructure()
- b.adjust()
- let bDescription = b.simpleDescription
练习:写一个实现这个接口的枚举。
注意声明SimpleStructure时候mutating关键字用来标记一个会修改结构体的方法。SimpleClass的声明不需要标记任何方法因为类中的方法经常会修改类。
使用extension来为现有的类型添加功能,比如添加一个计算属性的方法。你可以使用扩展来给任意类型添加协议,甚至是你从外部库或者框架中导入的类型。
- extension Int: ExampleProtocol {
- var simpleDescription: String {
- return "The number \(self)"
- }
- mutating func adjust() {
- self += 42
- }
- }
- 7.simpleDescription
练习:给Double类型写一个扩展,添加absoluteValue功能。
你可以像使用其他命名类型一样使用接口名——例如,创建一个有不同类型但是都实现一个接口的对象集合。当你处理类型是接口的值时,接口外定义的方法不可用。
- let protocolValue: ExampleProtocol = a
- protocolValue.simpleDescription
- // protocolValue.anotherProperty // Uncomment to see the error
即使protocolValue变量运行时的类型是simpleClass,编译器会把它的类型当做ExampleProtocol。这表示你不能调用类在它实现的接口之外实现的方法或者属性。