[整理]Swift的代理delegate,协议protocol

[protocol协议]

protocol:只定义,不实现

-》定义,只是为了说明,能做什么,即只有函数定义,没有函数实现

-》其他的,遵循,符合,此protocol协议的,类,结构体,枚举,自己去实现

-》任何实现了此协议的类型,都可以叫做 conform遵循 此协议

 

protocol定义:

protocol ExampleProtocol {
    var simpleDescription: String { get }
    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

[delegate,delegation代理]

代理是一种设计模式。

代理是允许一个类或结构体能够把其自己的一部分责任,转移,到另外一个类型的实例去,处理

这个类,被代理者,和,另外一个类型,代理者,之前的关系,就有点像:

一个普通人,和一个小管家:普通人的部分事情,要交由小管家去处理

这个代理模式,是通过:

定义一个协议,该协议封装了代理的职责(函数)

而实现的。

由此,遵循了类型,作为代理,保证提供了被代理的功能。

代理可以用于被响应特定的动作,或者(无需知道外部数据格式就可以)从外部源获取数据

 

举例:

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)
}

然后实现了DiceGame这个Protocol的类中,定义和实现了对应的DiceGameDelegate:

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 {
            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
        print("Rolled a \(diceRoll)")
    }
    func gameDidEnd(game: DiceGame) {
        print("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

 

相关参考资料

 

关于protocol,抽空先去看看官网的解释:

Start Developing iOS Apps (Swift): Learn the Essentials of Swift

The Swift Programming Language (Swift 2): Protocols

 

 

抽空去看看:

Swift 2 Tutorial Part 3: Tuples, Protocols, Delegates, and Table Views – Ray Wenderlich

貌似解释的还可以。

抽空好好学学这方面内容。

 

之前的整理内容,详见有道云笔记中的:

【整理】MVC架构和Target-Action,delegate,protocol的详细解释



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量