10.其餘主題

#10.1值和引用 值類型:結構,枚舉,Int,String,將值類型傳給方法時,將在內存中建立其副本,並傳遞其副本,方法能夠隨意修改它,不用擔憂原始值 引用類型:類,閉包,將閉包或對象傳遞給方法時,不會建立其副本,而是傳遞引用(內存地址)編程

##10.1.1引用循環swift

import UIKit
class Letter{
	let addressedTo : String
//	var mailbox : MailBox?
	weak var mailbox : MailBox?//弱引用斷開引用循環
	
	init(addressedTo : String) {
		self.addressedTo = addressedTo
	}

	deinit { //對象即將被釋放,且該對象佔用的內存被歸還給系統前調用。
		print("The letter addressed to \(addressedTo) is being discarded")
	}

}

class MailBox{
	let poNumber : Int
	var letter : Letter?
	
	init(poNumber : Int) {
		self.poNumber = poNumber
	}

	deinit {
		print("P.O. Box \(poNumber) is going away")
	}
}
//閉包中的引用循環
class MailChecker{
	let mailBox : MailBox
	let letter : Letter
	
	//lazy延遲屬性 用於推遲屬性的計算,直到屬性在代碼中被使用
	lazy var whoseMail: () -> String = {
		[unowned self] in//讓swift知道不該保留self對象,從而斷開引用循環
		return "Letter is addressed to \(self.letter.addressedTo)"
	
	}
	
	init(name : String) {
		self.mailBox = MailBox(poNumber : 311)
		self.letter = Letter(addressedTo : name)
	}
	deinit {
		print("class is being deinitialized")
	}
}

class ViewController: UIViewController {

	override func viewDidLoad() {
		super.viewDidLoad()
		self.view.backgroundColor = UIColor.blue
		//test
		var firstClassLetter : Letter?
		var homeMailBox : MailBox?
		
		firstClassLetter = Letter(addressedTo : "John Prestigiacomo")
		homeMailBox = MailBox(poNumber : 335)
		
		//互相引用沒法釋放
		firstClassLetter!.mailbox = homeMailBox
		homeMailBox!.letter = firstClassLetter
		
		//deinitialize the objects 若是爲循環引用,設置爲"nil",會調用"deinit"方法
		firstClassLetter = nil
		homeMailBox = nil
		
		var checker : MailChecker? = MailChecker(name : "Mark daimeng")
		let result : String = checker!.whoseMail()
		print(result)
		checker = nil
		
	}

	override func didReceiveMemoryWarning() {
		super.didReceiveMemoryWarning()
		// Dispose of any resources that can be recreated.
	}


}

#10.2邏輯運算符閉包

//邏輯非
var a : String = "pumpkin"
var b : String = "watermelon"

if !(a == b) {
	print("The strings don't match!")
}

//邏輯與
let c = true
let d = true

if c == true && d == true{

	print("both are true!")
}

//邏輯或
let e = true
let f = false

if e == true || f == true{

	print("one or the other is true!")
}

//泛型
func areValuesEuqal<T: Equatable>(value1: T,value2 : T) ->Bool{

	return value1 == value2
}

areValuesEuqal(value1: 1, value2: 2)//false
areValuesEuqal(value1: "a", value2: "a")//true
areValuesEuqal(value1: 3.12, value2: 3.12)//true

#10.3運算符重載 (擴展運算符) 使用運算符重載可以讓自定義類或結構支持加減乘除等基本數學運算,方法是類或結構中從新定義這些運算符的行爲ide

struct Matrix2x2{
	var a11 = 0.0,a12 = 0.0
	var a21 = 0.0,a22 = 0.0
}

func + (left : Matrix2x2,right : Matrix2x2) -> Matrix2x2{
	return Matrix2x2(a11:left.a11 + right.a11,
	                 a12:left.a12 + right.a12,
	                 a21:left.a21 + right.a21,
	                 a22:left.a22 + right.a22)

}

func * (left : Matrix2x2,right : Matrix2x2) -> Matrix2x2{
	return Matrix2x2(a11:left.a11 * right.a11 + left.a12 * right.a21,
	                 a12:left.a11 * right.a12 + left.a12 * right.a22,
	                 a21:left.a21 * right.a11 + left.a12 * right.a21,
	                 a22:left.a21 * right.a12 + left.a22 * right.a22)
	
}

var A : Matrix2x2 = Matrix2x2(a11 : 1,a12 :3,a21: 5,a22 :6)
var B : Matrix2x2 = Matrix2x2(a11 : 2,a12 :6,a21: 4,a22 :8)
var C = A + B //3 9 9 14

var E : Matrix2x2 = Matrix2x2(a11 :2,a12 :3,a21 :1,a22:4)
var F : Matrix2x2 = Matrix2x2(a11 :3,a12 :2,a21 :1,a22:-6)

var G = E * F //9 -14 6 -22

#10.4相等和相同rest

class Test1{
}
class Test2{
}

var t1 : Test1 = Test1()
var t2 : Test2 = Test2()
var t3 : Test2 = Test2()
var t4 = t2

t1 === t2 //false
t2 === t3 //false t2,t3指向Test2的不一樣實例
t4 === t2 //true
t4 !== t2 //false

對象相同性比較基於對象而不是類,即使兩個變量指向的對象屬於同一個類,只要他們指向的是不一樣的對象,也將被視爲不一樣code

#10.5Swift腳本編程 ##10.5.1建立腳本文件 打開Xcode->File->New->File->Other->Shell Script對象

###10.5.1運行腳本ip

./
./swiftScript.sh(文件名)
啓動REPL命令
  #!/usr/bin/env xcrun swift
相關文章
相關標籤/搜索