上QQ阅读APP看书,第一时间看更新
Declaring a protocol
You declare protocols using the protocol keyword, as follows:
protocol Toggling {
mutating func toggle()
}
Now that this protocol has been declared, any time that you declare a type conforming to Toggling, you'll be required to implement a mutating toggle() function.
You can use protocols in your type declarations, method declarations, or variable declarations. While it is technically possible to use protocols as interfaces for your objects or structs, that is usually not how they are used in Swift. Often, you will find yourself conforming to protocols when declaring your custom types or later in your code base, part of extending your existing type to bring additional functionality to it.