Swift-代理-delegate
2015-02-02 20:47
23 查看
//*****************************************************************
//
//
代理:delegate
//
代理是一种设计模式,它允许类或结构体将一些需要他们负责的功能交给其他的类型
//
代理的实现:定义协议来封装那些需要被代理的函数和方法,使其遵循者拥有这些被
//
代理的函数和方法
//
//*****************************************************************
//
//
下面的协议和类是前期准备
//
//**************************
protocol ProtocolRandom {
func random() -> Double
}
class Dice {
let sides: Int
let generator: ProtocolRandom
init(sides: Int,generator:
ProtocolRandom) {
self.sides= sides
self.generator= generator
}
func roll() -> Int{
return Int(generator.random()*
Double(sides)+
1 )
}
}
class Gener:
ProtocolRandom {
var lastRandom = 42.0
let m = 1234.56,a =
2233.11, c = 451.23
func random() -> Double{
lastRandom = (lastRandom *
a + c) %
m
return lastRandom/
m
}
}
//**************************
//
//
下面是两个基于色子游戏的协议
// 1.DiceGame协议可以在任意含有色子的游戏中实现
// 2.DiceGameDelegate协议用来追踪DiceGame的游戏过程
//
//**************************
protocol DiceGame {
var dice: Dice{
get }
func play()
}
//**************************************************************
//
// DiceGameDelegate提供了3个方法跟踪游戏过程,分别是开始,每轮摇色及结束
//
//**************************************************************
protocol DiceGameDelegate {
func gameDidStart (game: DiceGame)
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll:
Int)
func gameDidEnd(game: DiceGame)
}
//**************************************************************
//
// delegate是一个遵循DiceGameDelegate协议的可选属性
//
在func()方法中调用代理方法gameDidStart、game和gameDidEnd进行游戏跟踪
//
//**************************************************************
class SnakesAndLadders:
DiceGame{
let finalSquare = 25
let dice = Dice(sides:7, generator:
Gener())
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:
breakgameLoop
case let newSquare
wherenewSquare > finalSquare:
continue gameLoop
default:
square+= diceRoll
square+= board[square]
}
}
delegate?.gameDidEnd(self)
}
func test(){ println("Testing!")}
}
//**************************************************************
//
// DiceGameTracker实现了DiceGameDelegate协议的方法要求,用于跟踪游戏过程
//
跟踪时,传递被跟踪对象DiceGame过来给DiceGameTracker调用,也就是代理DiceGame
//
进行另外的工作(游戏跟踪)
//
//**************************************************************
class DiceGameTracker:
DiceGameDelegate{
var numberOfTurns = 0
func gameDidStart(game: DiceGame){
numberOfTurns =
0
if game isSnakesAndLadders {
println("Starteda new game of
蛇与梯子!")
}
if letgameon = game
as? SnakesAndLadders{
gameon.test()
}
println("the game is usinga
\(game.dice.sides)-sides dice")
}
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll:
Int) {
++numberOfTurns
println("Rollednumber = \(diceRoll)")
}
func gameDidEnd(game: DiceGame){
println("The game lastedfor
\(numberOfTurns) turns")
}
}
let tracker =
DiceGameTracker()
let game =
SnakesAndLadders()
game.delegate =
tracker
game.play()
//运行结果:
//Started a new game of
蛇与梯子!
//Testing!
//thegame is using a 7-sides dice
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 2
//Rollednumber = 4
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 1
//Rollednumber = 6
//Rollednumber = 4
//Thegame lasted for 9 turns
//
//
代理:delegate
//
代理是一种设计模式,它允许类或结构体将一些需要他们负责的功能交给其他的类型
//
代理的实现:定义协议来封装那些需要被代理的函数和方法,使其遵循者拥有这些被
//
代理的函数和方法
//
//*****************************************************************
//
//
下面的协议和类是前期准备
//
//**************************
protocol ProtocolRandom {
func random() -> Double
}
class Dice {
let sides: Int
let generator: ProtocolRandom
init(sides: Int,generator:
ProtocolRandom) {
self.sides= sides
self.generator= generator
}
func roll() -> Int{
return Int(generator.random()*
Double(sides)+
1 )
}
}
class Gener:
ProtocolRandom {
var lastRandom = 42.0
let m = 1234.56,a =
2233.11, c = 451.23
func random() -> Double{
lastRandom = (lastRandom *
a + c) %
m
return lastRandom/
m
}
}
//**************************
//
//
下面是两个基于色子游戏的协议
// 1.DiceGame协议可以在任意含有色子的游戏中实现
// 2.DiceGameDelegate协议用来追踪DiceGame的游戏过程
//
//**************************
protocol DiceGame {
var dice: Dice{
get }
func play()
}
//**************************************************************
//
// DiceGameDelegate提供了3个方法跟踪游戏过程,分别是开始,每轮摇色及结束
//
//**************************************************************
protocol DiceGameDelegate {
func gameDidStart (game: DiceGame)
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll:
Int)
func gameDidEnd(game: DiceGame)
}
//**************************************************************
//
// delegate是一个遵循DiceGameDelegate协议的可选属性
//
在func()方法中调用代理方法gameDidStart、game和gameDidEnd进行游戏跟踪
//
//**************************************************************
class SnakesAndLadders:
DiceGame{
let finalSquare = 25
let dice = Dice(sides:7, generator:
Gener())
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:
breakgameLoop
case let newSquare
wherenewSquare > finalSquare:
continue gameLoop
default:
square+= diceRoll
square+= board[square]
}
}
delegate?.gameDidEnd(self)
}
func test(){ println("Testing!")}
}
//**************************************************************
//
// DiceGameTracker实现了DiceGameDelegate协议的方法要求,用于跟踪游戏过程
//
跟踪时,传递被跟踪对象DiceGame过来给DiceGameTracker调用,也就是代理DiceGame
//
进行另外的工作(游戏跟踪)
//
//**************************************************************
class DiceGameTracker:
DiceGameDelegate{
var numberOfTurns = 0
func gameDidStart(game: DiceGame){
numberOfTurns =
0
if game isSnakesAndLadders {
println("Starteda new game of
蛇与梯子!")
}
if letgameon = game
as? SnakesAndLadders{
gameon.test()
}
println("the game is usinga
\(game.dice.sides)-sides dice")
}
func game(game: DiceGame,didStartNewTurnWithDiceRoll diceRoll:
Int) {
++numberOfTurns
println("Rollednumber = \(diceRoll)")
}
func gameDidEnd(game: DiceGame){
println("The game lastedfor
\(numberOfTurns) turns")
}
}
let tracker =
DiceGameTracker()
let game =
SnakesAndLadders()
game.delegate =
tracker
game.play()
//运行结果:
//Started a new game of
蛇与梯子!
//Testing!
//thegame is using a 7-sides dice
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 2
//Rollednumber = 4
//Rollednumber = 3
//Rollednumber = 4
//Rollednumber = 1
//Rollednumber = 6
//Rollednumber = 4
//Thegame lasted for 9 turns
相关文章推荐
- Swift 中的代理 (Delegate)
- Swift入门教程18-代理Delegate
- Swift_协议、代理(protocol、delegate可选@objc optional与必需、协议扩展、is as? as!检查协议)
- swift之代理传值(delegate的用法)
- Swift之delegate代理和Block闭包的反向传值
- Swift中Delegate代理声明和实现
- swift delegate代理和block的使用
- swift-delegate(代理)或者block传值
- 【swift_4】swift之代理传值(delegate的用法)
- iOS开发之OC篇(13)—— Protocol(协议)、Delegate(代理)
- swift -NavigationController,代理传值
- swift基础学习传值[属性传值、代理、block代码块、等]
- swift协议代理的使用以及解决循环引用问题
- C#中的代理(Delegate)
- ios delegate代理,多个/不同/两个view之间传递数据,传递批量数据,多个数据
- Delegate 代理委托 +block+类别Category
- delegate 代理的三种调用方法
- Swift-代理+协议[初解]
- swift 获取AppDelegate对象
- Swift_TableView(delegate,dataSource,prefetchDataSource 详解)