1.協議的語法
定義協議:
protocol SomeProtocol {
// protocol definition goes here
}
遵守協議:
struct SomeStructure: FirstProtocol, AnotherProtocol {
// structure definition goes here
}
當一個類既有父類,又遵守其他協議時,將父類名寫在所遵守協議的前面:
class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {
// class definition goes here
}
2.屬性的要求
- 在協議中,實例屬性總是使用
var
聲明爲變量屬性。可讀的與可設置的屬性在類型聲明後通過寫入{get set}表示,可讀的屬性通過寫入{get}表示。
protocol SomeProtocol {
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
- 在協議中,類屬性始終使用
static
關鍵字作爲類屬性聲明的前綴。在類中實現時,可以使用class
或static
關鍵字作類屬性聲明前綴。
protocol AnotherProtocol {
static var someTypeProperty: Int { get set }
}
示例:
protocol FullyNamed {
var fullName: String { get }
}
struct Person: FullyNamed {
var fullName: String
}
let john = Person(fullName: "John Appleseed")
// john.fullName is "John Appleseed"
class Starship: FullyNamed {
var prefix: String?
var name: String
init(name: String, prefix: String? = nil) {
self.name = name
self.prefix = prefix
}
var fullName: String {
return (prefix != nil ? prefix! + " " : "") + name
}
}
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
// ncc1701.fullName is "USS Enterprise"
3.方法的要求
- 在協議中聲明的實例方法和類方法沒有方法體,允許可變參數,但是不能爲協議中的方法參數指定默認值;
- 在協議中,類方法始終使用
static
關鍵字作爲前綴。在類中實現時,可以使用class
或static
關鍵字作類方法聲明前綴。
protocol SomeProtocol {
static func someTypeMethod()
}
示例:
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
print("Here's a random number: \(generator.random())")
// Prints "Here's a random number: 0.3746499199817101"
print("And another one: \(generator.random())")
// Prints "And another one: 0.729023776863283"
- 在協議中,可變實例方法使用關鍵字
mutating
作前綴。在類中實現該方法時不需要寫mutating
關鍵字。mutating
關鍵字僅供結構體和枚舉使用。
protocol Togglable {
mutating func toggle()
}
enum OnOffSwitch: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
var lightSwitch = OnOffSwitch.off
lightSwitch.toggle()
// lightSwitch is now equal to .on
5.構造器的要求
- 協議可以要求符合的類型來實現特定的構造器。可以將這些構造器作爲協議定義的一部分。
protocol SomeProtocol {
init(someParameter: Int)
}
- 在符合構造器協議的類中,可以將協議構造器作爲該類的指定構造器或便利構造器。在這兩種情況中,必須使用
required
修飾這些構造器。required
修飾符確保在該類的所有子類上提供構造器的顯式或繼承的實現。
class SomeClass: SomeProtocol {
required init(someParameter: Int) {
// initializer implementation goes here
}
}
注意:
在使用final
關鍵字修飾的類上,不需要使用required
關鍵字標記協議構造器的實現。
- 如果子類重寫父類的一個指定構造器,同時還從協議中實現相匹配的構造器,則該構造器需要使用
required
和override
關鍵字修飾。
protocol SomeProtocol {
init()
}
class SomeSuperClass {
init() {
// initializer implementation goes here
}
}
class SomeSubClass: SomeSuperClass, SomeProtocol {
// "required" from SomeProtocol conformance; "override" from SomeSuperClass
required override init() {
// initializer implementation goes here
}
}
6.協議是一種類型
協議是一種類型:
- 作爲函數、方法或構造器中的參數類型或返回值類型;
- 作爲常量、變量或屬性的類型
- 作爲數組、字典或其他容器中的元素類型
示例:
定義一個新類Dice
,它表示用於棋盤遊戲的n面骰子。Dice
實例有一個sides
的整數屬性,它表示有多少邊;還有一個類型爲RandomNumberGenerator
的屬性generator
,它提供了一個隨機數生成器,可以從中生成骰子擲骰值,可以將其設置爲遵守RandomNumberGenerator
協議的任何類型的實例。
class Dice {
let sides: Int
let generator: RandomNumberGenerator
init(sides: Int, generator: RandomNumberGenerator) {
self.sides = sides
self.generator = generator
}
func roll() -> Int {
return Int(generator.random() * Double(sides)) + 1
}
}
以下是如何使用Dice
類創建一個帶有 LinearCongruentialGenerator
實例作爲其隨機數生成器的六面骰子:
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for _ in 1...5 {
print("Random dice roll is \(d6.roll())")
}
// Random dice roll is 3
// Random dice roll is 5
// Random dice roll is 4
// Random dice roll is 5
// Random dice roll is 4
注意:
因爲協議是一種類型,所以以大寫字母開頭,以匹配Swift中其他類型的名稱(如Int、String和Double)。
7.委託
委託是一種設計模式,它允許類或結構體將其部分職責傳遞(或委託)給另一種類型的實例。這種設計模式是通過定義一個協議來實現的,該協議封裝了委託者的職責,這樣就保證了一致性類型(稱爲委託)能夠提供已經委託的功能。委託可以用來響應特定的操作,或者從外部源檢索數據,而不需要知道該源的底層類型。
示例:
DiceGame
規定了涉及骰子的遊戲所採用的協議,使用DiceGameDelegate
協議可以追蹤DiceGame
的進度:
protocol DiceGame {
var dice: Dice { get }
func play()
}
protocol DiceGameDelegate: AnyObject {
func gameDidStart(_ game: DiceGame)
func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
func gameDidEnd(_ game: DiceGame)
}
定義一個類SnakesAndLadders
遵守DiceGame協議,並通知DiceGameDelegate
其進度:
class SnakesAndLadders: DiceGame {
let finalSquare = 25
let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
var square = 0
var board: [Int]
init() {
board = Array(repeating: 0, count: finalSquare + 1)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
}
weak var delegate: DiceGameDelegate?
func play() {
square = 0
delegate?.gameDidStart(self)
gameLoop: while square != finalSquare {
let diceRoll = dice.roll()
delegate?.game(self, didStartNewTurnWithDiceRoll: diceRoll)
switch square + diceRoll {
case finalSquare:
break gameLoop
case let newSquare where newSquare > finalSquare:
continue gameLoop
default:
square += diceRoll
square += board[square]
}
}
delegate?.gameDidEnd(self)
}
}
下面示例顯示了一個名爲DiceGameTracker
的類,它遵守DiceGameDelegate
協議:
class DiceGameTracker: DiceGameDelegate {
var numberOfTurns = 0
func gameDidStart(_ game: DiceGame) {
numberOfTurns = 0
if game is SnakesAndLadders {
print("Started a new game of Snakes and Ladders")
}
print("The game is using a \(game.dice.sides)-sided dice")
}
func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) {
numberOfTurns += 1
print("Rolled a \(diceRoll)")
}
func gameDidEnd(_ game: DiceGame) {
print("The game lasted for \(numberOfTurns) turns")
}
}
以下是DiceGameTracker
的實際應用:
let tracker = DiceGameTracker()
let game = SnakesAndLadders()
game.delegate = tracker
game.play()
// Started a new game of Snakes and Ladders
// The game is using a 6-sided dice
// Rolled a 3
// Rolled a 5
// Rolled a 4
// Rolled a 5
// The game lasted for 4 turns
8.使用擴展添加協議一致性
- 當在擴展中將一致性添加到實例的類型時,類型的現有實例自動採用並遵守協議。
示例:
這個被稱爲TextRepresentable的協議,可以由任何能夠表示爲文本的類型實現。這可能是對其自身的描述,或其當前狀態的文本版本:
protocol TextRepresentable {
var textualDescription: String { get }
}
上面的Dice
類可以擴展爲採用並遵守TextRepresentable
協議:
extension Dice: TextRepresentable {
var textualDescription: String {
return "A \(sides)-sided dice"
}
}
任何Dice
類的實例現在可以被視爲TextRepresentable
:
let d12 = Dice(sides: 12, generator: LinearCongruentialGenerator())
print(d12.textualDescription)
// Prints "A 12-sided dice"
同樣,SnakesAndLadders
類也可以擴展爲採用和遵守TextRepresentable
協議:
extension SnakesAndLadders: TextRepresentable {
var textualDescription: String {
return "A game of Snakes and Ladders with \(finalSquare) squares"
}
}
print(game.textualDescription)
// Prints "A game of Snakes and Ladders with 25 squares"
- 泛型類型只能在特定條件下才能滿足協議的要求,比如類型的泛型參數符合協議。在擴展類型時,可以通過列出約束條件使泛型類型在特定條件下符合協議。通過編寫泛型where語句,在採用的協議名稱之後添加這些約束。
示例:
下面的擴展使數組實例在存儲符合TextRepresentable
類型的元素時符合TextRepresentable
協議。
extension Array: TextRepresentable where Element: TextRepresentable {
var textualDescription: String {
let itemsAsText = self.map { $0.textualDescription }
return "[" + itemsAsText.joined(separator: ", ") + "]"
}
}
let myDice = [d6, d12]
print(myDice.textualDescription)
// Prints "[A 6-sided dice, A 12-sided dice]"
- 如果一個類型已經符合協議的所有要求,但是還未聲明採用了該協議,則可以讓它採用一個空擴展的協議:
struct Hamster {
var name: String
var textualDescription: String {
return "A hamster named \(name)"
}
}
extension Hamster: TextRepresentable {}
現在,只要TextRepresentable
是所需類型,就可以使用Hamster
實例:
let simonTheHamster = Hamster(name: "Simon")
let somethingTextRepresentable: TextRepresentable = simonTheHamster
print(somethingTextRepresentable.textualDescription)
// Prints "A hamster named Simon"
注意:
類型不會僅通過滿足其要求而自動採用協議。 它們必須始終明確地聲明它們採用了協議。
9.協議類型的集合
- 協議可以用作存儲在數組或字典等集合中的類型。
示例:
這個例子創建了一個元素類型爲TextRepresentable
的數組:
let things: [TextRepresentable] = [game, d12, simonTheHamster]
for thing in things {
print(thing.textualDescription)
}
// A game of Snakes and Ladders with 25 squares
// A 12-sided dice
// A hamster named Simon
10.協議的繼承
- 協議可以繼承一個或多個其他協議,並且可以在它繼承的需求之上添加更多的需求。協議繼承的語法類似於類繼承的語法,但是可以列出多個繼承的協議,用逗號分隔:
protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
// protocol definition goes here
}
下面是一個繼承TextRepresentable
協議的例子:
protocol PrettyTextRepresentable: TextRepresentable {
var prettyTextualDescription: String { get }
}
SnakesAndLadders
類通過擴展來採用和遵守PrettyTextRepresentable
協議:
extension SnakesAndLadders: PrettyTextRepresentable {
var prettyTextualDescription: String {
var output = textualDescription + ":\n"
for index in 1...finalSquare {
switch board[index] {
case let ladder where ladder > 0:
output += "▲ "
case let snake where snake < 0:
output += "▼ "
default:
output += "○ "
}
}
return output
}
}
現在可以使用prettyTextualDescription
屬性打印任何SnakesAndLadders
實例的漂亮文本描述:
print(game.prettyTextualDescription)
// A game of Snakes and Ladders with 25 squares:
// ○ ○ ▲ ○ ○ ▲ ○ ○ ▲ ▲ ○ ○ ○ ▼ ○ ○ ○ ○ ▼ ○ ○ ▼ ○ ▼ ○
11.僅支持類的協議
- 通過將AnyObject協議添加到協議的繼承列表中,來限制該協議只適用於
class
類型,而不適用於枚舉和結構體。
protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
// class-only protocol definition goes here
}
12.協議組合
- 當需要一個類型同時符合多個協議時,可以使用協議組合,將多個協議組合到單個需求中。
- 協議組合不是定義新的協議類型;
- 協議組合還可以包含一個類類型,可以使用它來指定所需的超類;
- 協議組合使用
&
符連接多個協議;
示例:
函數中的參數celebrator
要求傳入的參數類型必須同時遵守Named
和Aged
協議:
protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person: Named, Aged {
var name: String
var age: Int
}
func wishHappyBirthday(to celebrator: Named & Aged) {
print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(to: birthdayPerson)
// Prints "Happy birthday, Malcolm, you're 21!"
下面的示例,它將前面示例中的Named
協議與Location
類組合起來:
class Location {
var latitude: Double
var longitude: Double
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
class City: Location, Named {
var name: String
init(name: String, latitude: Double, longitude: Double) {
self.name = name
super.init(latitude: latitude, longitude: longitude)
}
}
func beginConcert(in location: Location & Named) {
print("Hello, \(location.name)!")
}
let seattle = City(name: "Seattle", latitude: 47.6, longitude: -122.3)
beginConcert(in: seattle)
// Prints "Hello, Seattle!"
13. 檢查協議一致性
- 可以使用
is
和as
操作符來檢查協議的一致性,並強制轉換到特定的協議。 - 如果一個實例符合協議,則
is
操作符返回true;否則返回false。 -
as?
操作符返回協議類型的可選值,如果實例不符合該協議,則該值爲nil。 -
as!
操作符將強制轉換爲協議類型,並在轉換失敗時觸發運行時錯誤。
示例:
protocol HasArea {
var area: Double { get }
}
class Circle: HasArea {
let pi = 3.1415927
var radius: Double
var area: Double { return pi * radius * radius }
init(radius: Double) { self.radius = radius }
}
class Country: HasArea {
var area: Double
init(area: Double) { self.area = area }
}
class Animal {
var legs: Int
init(legs: Int) { self.legs = legs }
}
let objects: [AnyObject] = [
Circle(radius: 2.0),
Country(area: 243_610),
Animal(legs: 4)
]
for object in objects {
if let objectWithArea = object as? HasArea {
print("Area is \(objectWithArea.area)")
} else {
print("Something that doesn't have an area")
}
}
// Area is 12.5663708
// Area is 243610.0
// Something that doesn't have an area
14.可選協議
- 當協議中某些方法或屬性不需要遵守協議的類型實現時,使用關鍵字
optional
來指明; - 協議和可選需求都必須用
@objc
屬性標記; -
@objc
協議只能由繼承自Objective-C類或其他@objc
類的類使用,結構體或枚舉不能使用。
示例:
@objc protocol CounterDataSource {
@objc optional func increment(forCount count: Int) -> Int
@objc optional var fixedIncrement: Int { get }
}
class Counter {
var count = 0
var dataSource: CounterDataSource?
func increment() {
if let amount = dataSource?.increment?(forCount: count) {
count += amount
} else if let amount = dataSource?.fixedIncrement {
count += amount
}
}
}
class ThreeSource: NSObject, CounterDataSource {
let fixedIncrement = 3
}
var counter = Counter()
counter.dataSource = ThreeSource()
for _ in 1...4 {
counter.increment()
print(counter.count)
}
// 3
// 6
// 9
// 12
class TowardsZeroSource: NSObject, CounterDataSource {
func increment(forCount count: Int) -> Int {
if count == 0 {
return 0
} else if count < 0 {
return 1
} else {
return -1
}
}
}
counter.count = -4
counter.dataSource = TowardsZeroSource()
for _ in 1...5 {
counter.increment()
print(counter.count)
}
// -3
// -2
// -1
// 0
// 0
15.協議擴展
- 可以對協議進行擴展,爲遵守協議的類型提供方法、構造器、下標和計算屬性的實現。
- 協議擴展可以爲遵守協議的類型添加實現,但它不能使協議從另一個協議擴展或繼承。協議繼承始終在協議聲明本身指定。
示例:
RandomNumberGenerator
協議通過擴展提供一個randomBool()
方法,該方法使用random()
的結果返回一個Bool
值。
extension RandomNumberGenerator {
func randomBool() -> Bool {
return random() > 0.5
}
}
通過在協議中創建擴展,所有遵守該協議的類型將自動獲得該方法的實現,而不需要添加額外的修改。
let generator = LinearCongruentialGenerator()
print("Here's a random number: \(generator.random())")
// Prints "Here's a random number: 0.3746499199817101"
print("And here's a random Boolean: \(generator.randomBool())")
// Prints "And here's a random Boolean: true"
- 如果協議擴展爲該協議的任何方法或計算屬性提供了默認實現,同時遵守協議的類型也提供了所需方法或屬性的自身實現,則將使用類型本身的實現而不是協議擴展提供的實現。
示例:
extension PrettyTextRepresentable {
var prettyTextualDescription: String {
return textualDescription
}
}
- 在定義協議擴展時,可以指定符合類型必須滿足的約束條件,然後才能使用擴展的方法和屬性。通過編寫泛型
where
語句,在要擴展的協議名稱添加這些約束。
示例:
extension Collection where Element: Equatable {
func allEqual() -> Bool {
for element in self {
if element != self.first {
return false
}
}
return true
}
}
let equalNumbers = [100, 100, 100, 100, 100]
let differentNumbers = [100, 100, 200, 100, 200]
print(equalNumbers.allEqual())
// Prints "true"
print(differentNumbers.allEqual())
// Prints "false"