协议 (Protocols)
用于统一方法和属性的名称,而不实现任何功能,能够被类、枚举、结构体实现,满足协议要求的都成为协议的遵循者,遵循者需要提供协议指定的成员,如方法,属性,操作符,下标。
委托可以响应特定的动作和接受外部数据源提供的数据,而无需知道外部数据源的类型
用于统一方法和属性的名称,而不实现任何功能,能够被类、枚举、结构体实现,满足协议要求的都成为协议的遵循者,遵循者需要提供协议指定的成员,如方法,属性,操作符,下标。
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.属性要求
协议要求其遵循者提供一个实例属性和一个类属性,不要求是否有存储属性或计算属性,只指定所需的属性名称和类型protocol SomeProtocol {
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
// 用类实现协议时,用class关键字表示该属性为类成员,用结构体或枚举实现协议时,用static关键字表示
protocol AnotherProtocol {
class 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 ? prefix! + " " : "") + name
}
}
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
// ncc1701.fullName is "USS Enterprise"
3.方法要求
协议要求其遵循者必须具备某种特定的实例方法和类方法,协议方法的声明与普通方法类似,但不需要方法的内容,协议方法支持变长参数,不支持默认参数protocol SomeProtocol {
class 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) % m)
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
println("Here's a random number: \(generator.random())")
// prints "Here's a random number: 0.37464991998171"
println("And another one: \(generator.random())")
// prints "And another one: 0.729023776863283"
4.突变方法要求 Mutating Method Requirements
能在方法或函数内部改变实例类型的方法成为突变方法,在值类型中的函数前缀加上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.协议类型
协议本身不实现任何功能,但仍可以当做类型来使用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
}
}
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for _ in 1...5 {
println("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
6.委托(代理)
委托是一种设计模式,允许类或结构体将一些需要他们负责的功能委托给其他类型,只需定义协议来封装需要被委托的行数和方法,使其遵循者拥有这些被委托的函数和方法委托可以响应特定的动作和接受外部数据源提供的数据,而无需知道外部数据源的类型
// 以下是控制流章节的掷骰子的委托方式写法
protocol DiceGame {
var dice: Dice { get }
func play()
}
protocol DiceGameDelegate {
func gameDidStart(game: DiceGame)
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
func gameDidEnd(game: DiceGame)
}
class SnakesAndLadders: DiceGame {
let finalSquare = 25
let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
var square = 0
var board: Int[]
init() {
board = Int[](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
}
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)
}
}
class DiceGameTracker: DiceGameDelegate {
var numberOfTurns = 0
func gameDidStart(game: DiceGame) {
numberOfTurns = 0
if game is SnakesAndLadders {
println("Started a new game of Snakes and Ladders")
}
println("The game is using a \(game.dice.sides)-sided dice")
}
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) {
++numberOfTurns
println("Rolled a \(diceRoll)")
}
func gameDidEnd(game: DiceGame) {
println("The game lasted for \(numberOfTurns) turns")
}
}
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
7.在扩展中添加协议成员
在无法修改源代码的情况下,依然可以通过通过扩展来扩充已存在的类型,扩展可以为已存在的类型添加属性,方法,下标,协议等成员。// TextRepresentable 协议含有一个asText方法
protocol TextRepresentable {
func asText() -> String
}
// 通过扩展为Dice类遵循 TextRepresentable 协议,之后Dice类型的实例就可以当做 TextRepresentable类型
extension Dice: TextRepresentable {
func asText() -> String {
return "A \(sides)-sided dice"
}
}
let d12 = Dice(sides: 12, generator: LinearCongruentialGenerator())
println(d12.asText())
// prints "A 12-sided dice"
// SnakeAndLadders 通过扩展方式遵循TextRepresentable协议
extension SnakesAndLadders: TextRepresentable {
func asText() -> String {
return "A game of Snakes and Ladders with \(finalSquare) squares"
}
}
println(game.asText())
// prints "A game of Snakes and Ladders with 25 squares"
7.1通过扩展补充协议声明
当一个类型已经实现了协议中的所有要求,却没有声明,可以通过扩展来补充协议声明struct Hamster {
var name: String
func asText() -> String {
return "A hamster named \(name)"
}
}
extension Hamster: TextRepresentable {}
let simonTheHamster = Hamster(name: "Simon")
let somethingTextRepresentable: TextRepresentable = simonTheHamster
println(somethingTextRepresentable.asText())
// prints "A hamster named Simon"
8.集合中的协议
协议类型可以被集合使用,表示集合中的所有元素均为协议类型let things: TextRepresentable[] = [game, d12, simonTheHamster]
for thing in things {
println(thing.asText())
}
// A game of Snakes and Ladders with 25 squares
// A 12-sided dice
// A hamster named Simon
9.协议的继承
协议能够继承一个或多个其他协议,多个协议用逗号分隔protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
// protocol definition goes here
}
protocol PrettyTextRepresentable: TextRepresentable {
func asPrettyText() -> String
}
extension SnakesAndLadders: PrettyTextRepresentable {
func asPrettyText() -> String {
var output = asText() + ":\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
}
}
println(game.asPrettyText())
// A game of Snakes and Ladders with 25 squares:
// ○ ○ ▲ ○ ○ ▲ ○ ○ ▲ ▲ ○ ○ ○ ▼ ○ ○ ○ ○ ▼ ○ ○ ▼ ○ ▼ ○
10.协议的合成
一个协议可由多个协议进行组合,格式 protocol<SomeProtocol, AnotherProtocol>,协议的合成并不会生成一个新的协议类型,而是将多个协议合成一个临时的协议,超出范围后立即失效protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person: Named, Aged {
var name: String
var age: Int
}
func wishHappyBirthday(celebrator: protocol<Named, Aged>) {
println("Happy birthday \(celebrator.name) - you're \(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(birthdayPerson)
// prints "Happy birthday Malcolm - you're 21!"
11.检查协议的一致性
使用is检验协议一致性,是否遵循了某个协议,使用as将协议类型向下强制转换为其他的协议类型,as?返回一个可选值,当实例遵循某种类型时,返回该协议类型,否则返回nil@objc protocol HasArea {
var area: Double { get }
}
// Circle和Country都遵循了HasArea协议
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 }
}
// Animal没有实现任何协议
class Animal {
var legs: Int
init(legs: Int) { self.legs = legs }
}
// 采用AnyObject来装载他们的实例
let objects: AnyObject[] = [
Circle(radius: 2.0),
Country(area: 243_610),
Animal(legs: 4)
]
// 迭代检查数组元素是否遵循某种协议
for object in objects {
if let objectWithArea = object as? HasArea {
println("Area is \(objectWithArea.area)")
} else {
println("Something that doesn't have an area")
}
}
// Area is 12.5663708
// Area is 243610.0
// Something that doesn't have an area
12.可选协议要求
在协议中使用 @optional 关键字前缀表示可选成员,让遵循者选择是否实现这些成员,可选成员在调用时使用可选链// CounterDataSource 含有incrementForCount的可选方法和fixedIncrement的可选属性
@objc protocol CounterDataSource {
@optional func incrementForCount(count: Int) -> Int
@optional var fixedIncrement: Int { get }
}
// Couner含有 CounterDataSource? 类型的可选属性 dataSource
@objc class Counter {
var count = 0
var dataSource: CounterDataSource?
func increment() {
if let amount = dataSource?.incrementForCount?(count) {
count += amount
} else if let amount = dataSource?.fixedIncrement? {
count += amount
}
}
}
class ThreeSource: CounterDataSource {
let fixedIncrement = 3
}
var counter = Counter()
counter.dataSource = ThreeSource()
for _ in 1...4 {
counter.increment()
println(counter.count)
}
// 3
// 6
// 9
// 12
class TowardsZeroSource: CounterDataSource {
func incrementForCount(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()
println(counter.count)
}
// -3
// -2
// -1
// 0
// 0