##6.1協議閉包
/* protocol ProtocolName{ 協議的成員 } */ protocol NewLockUnlockProtocol{ //get 和 set 是特殊限定符,在協議中同時使用了set和get時,遵循協議的類或結構中,必須使用關鍵字var來聲明相應的變量。若是隻使用了get,則在遵循協議的類或結構中,聲明變量時,可以使用關鍵字let,也可以使用關鍵字var var locked : Bool{get set} func lock() -> String func unlock() -> String } class Safe : NewLockUnlockProtocol{ var locked : Bool = false func lock() -> String{ locked = true return "Ding" } func unlock() -> String { locked = false return "Dong" } } class Gate : NewLockUnlockProtocol{ var locked: Bool = false func lock() -> String { locked = true return "Clink" } func unlock() -> String { locked = false return "Clonk" } } let mySafe = Safe() mySafe.lock()//Ding mySafe.unlock()//Dong let myGate = Gate() myGate.lock()//Clink myGate.unlock()//Clonk
#6.2遵循多個協議app
protocol AreaComputationProtocol{ func computeArea() ->Double } protocol PerimeterComputationProtocol{ func computePerimeter() ->Double } struct Rectangle : AreaComputationProtocol,PerimeterComputationProtocol{ var width,height : Double func computeArea() -> Double { return width * height } func computePerimeter() -> Double { return width * 2 + height * 2 } } var square = Rectangle(width : 3,height : 3) var rectangle = Rectangle(width : 4,height : 6) square.computeArea()//9 square.computePerimeter()//12 rectangle.computeArea()//24 rectangle.computePerimeter()//20
#6.3協議也可繼承this
protocol TriangleProtocol : AreaComputationProtocol,PerimeterComputationProtocol{ var a : Double {get set} var b : Double {get set} var c : Double {get set} var base : Double {get set} var height : Double {get set} } struct Triangle : TriangleProtocol{ var a,b,c : Double var base , height: Double func computeArea() -> Double { return (base * height)/2 } func computePerimeter() -> Double { return a + b + c } } var triangle1 = Triangle(a : 4,b : 5 ,c : 6,base : 12,height : 10) triangle1.computeArea()//60 triangle1.computePerimeter()//15
#6.4委託code
protocol VendingMachineProtocol{ var coinInserted : Bool {get set} func shouldVend() -> Bool } class Vendor : VendingMachineProtocol{ var coinInserted: Bool = false func shouldVend() -> Bool { if coinInserted == true{ coinInserted = false return true } return false } } class ColaMachine { var vendor : VendingMachineProtocol init(vendor : VendingMachineProtocol) { self.vendor = vendor } func insertCoin(){ vendor.coinInserted = true } func pressColaButton() -> String{ if vendor.shouldVend() == true{ return "Here is Cola" }else{ return "You must insert a coin!" } } func preesRootBeerButton() ->String{ if vendor.shouldVend() == true{ return "Here is a Root Beer" }else{ return "You must insert a coin" } } } var vendingMachine = ColaMachine(vendor : Vendor()) vendingMachine.pressColaButton()//You must insert a coin!" vendingMachine.insertCoin() vendingMachine.preesRootBeerButton()//Here is a Root Beer vendingMachine.insertCoin() vendingMachine.pressColaButton()//Here is Cola"
#6.5擴展對象
extension ColaMachine{ func pressDietColaButton()->String{ if vendor.shouldVend() == true{ return "Here is a Diet Cola!" }else{ return "You must insert a coin!" } } } var newVendingMachine = ColaMachine(vendor : Vendor()) newVendingMachine.pressDietColaButton()//"You must insert a coin!" newVendingMachine.insertCoin() newVendingMachine.pressDietColaButton()//Here is a Diet Cola!"
##6.5.1擴展基本類型 ###6.5.1 MB和GB繼承
extension Int{ //計算屬性 var kb : Int { return self * 1_024}//千字節 var mb : Int { return self * 1_024 * 1_024}//兆字節 var gb : Int { return self * 1_024 * 1_024 * 1_024}//吉字節 } var x : Int = 4.kb//4096 var y = 8.mb//8388608 var z = 2.gb//2147483648
###6.5.2溫度ip
extension Double{ var F : Double {return self} var C : Double {return (((self - 32.0) * 5.0) / 9.0)} var K : Double {return (((self - 32.0) / 1.8) + 273.15)} } var temperatureF = 80.4.F//80.40000000000001 var temperatureC = temperatureF.C//26.88888888888889 var temperatureK = temperatureF.K//300.0388888888889
###6.5.3給String類型添加方法get
extension String { func prependString (value : String) -> String{ return value + self } func appendString(value : String) -> String{ return self + value } } "x".prependString(value: "Prefix")//Prefixx "y".appendString(value: "Prefix")//yPrefix
###6.5.4使用關鍵字mutating mutating方法適用於修改被擴展類的對象的實際值string
extension Int{ mutating func triple(){ self = self * 3 } } var trip = 3 trip.triple()//9 extension String{ mutating func decorate(){ self = "*** " + self + " ***" } } var testString = "decorate this" testString.decorate()//"*** decorate this ***"
###6.5.6擴展中使用閉包it
extension Int{ func repeatMethod (work:()->()){ for _ in 0..<self{ work() } } } **work:() ->()不接受任何參數也不返回任何結果的代碼塊,參數名work** **"_"表示不在意,這裏不須要使用變量** 5.repeatMethod { print("repeat this string") } //repeat this string //repeat this string //repeat this string //repeat this string //repeat this string