字典
//定義:
var dict = ["abacus" : "算盤","abnormal" : "異常","hello" : "你好"]
//print(dict["abnormal"])//可空類型
//print(dict["abnormal"]!)//string 類型
//
//dict["shit"] = "狗屎"
//print(dict["shit"]!)
//print(dict)
//
////修改元素
//dict["shit"] = "牛糞"
//
////刪除元素
////第一種寫法
//dict.removeValueForKey("hello")
////第二種寫法
////dict["hello"] = nil
//print(dict)
//
////遍歷字典
//
////遍歷字典中的所有值
//for value in dict.values{
// print(value)
//}
//
//for key in dict.keys{
// print("\(key)-->\(dict[key])")
//}
//
////通過(guò)元組獲取字典的鍵值
//for (key ,value) in dict {
// print("\(key)--> \(value)")
//}
//
//
//// 哈希碼(hash code)/散列碼
//// MD5 / SHA-1//最經(jīng)典的2個(gè)hash算法
////創(chuàng)建集合:可以去除重復(fù)元素
//var a: Set<Int> = [1,2,3,1,2,5]
//print(a)
//
//a.insert(100)
//a.remove(2)
////集合的交集,差集,并集
//var b: Set<Int> = [3,5,7,9,11]
//
//print(a.intersect(b)) //交集
//print(a.union(b)) //并集
//print(a.subtract(b)) //差集 a里面有b里面沒(méi)有的
////判斷是不是它的子集
//print(b.isStrictSubsetOf(a))
//
//
////集合的遍歷
//for x in a {
// print(x,terminator:" ")
//}
////創(chuàng)建數(shù)組
//let b = [1,2,3,1,2,5]
//print(b)
日期告希,系統(tǒng)時(shí)間代碼
et date = NSDate()//可以取到現(xiàn)在的系統(tǒng)時(shí)間
// let cal = NSCalendar.currentCalendar()//拿到現(xiàn)在的日歷
// let hour = cal.component(.Hour, fromDate: date)
指紋解鎖代碼,新概念閉包的第一次應(yīng)用
override func viewDidLoad() {
super.viewDidLoad()
let errpoint = NSErrorPointer()
let ctx = LAContext()
//evalutepolicy方法的第三個(gè)參數(shù)是一個(gè)函數(shù)
// 該函數(shù)有兩個(gè)參數(shù)沒(méi)有返回值
// 給改參數(shù)傳參時(shí)可以在花括中寫一個(gè)匿名函數(shù)傳進(jìn)去
// 該匿名函數(shù)通常也稱之為閉包(closure)
if ctx.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: errpoint) {
ctx.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "請(qǐng)輸入指紋進(jìn)行支付", reply:
{ (isOk, err) -> Void in
if isOk {
print("支付成功")
}
else {
print("指紋驗(yàn)證失敗东抹!請(qǐng)輸入密碼")
}
})
}
else {
print("設(shè)備不支持指紋識(shí)別")
}
}
函數(shù)的幾種寫法
//函數(shù)的參數(shù)名
//函數(shù)名(外表參數(shù)名 內(nèi)部參數(shù)名: 類型,外部參數(shù)名 內(nèi)部參數(shù)名: 類型)
//可以使用_來(lái)作為外部參數(shù)名表示省略外部參數(shù)名
//func myMin(a x:Int, y:Int) -> Int {
// return x < y ? x : y
//}
////函數(shù)調(diào)用時(shí)要寫函數(shù)的外部參數(shù)名
//print(myMin(a: 3,y: 5))
// 定義函數(shù)
// func 函數(shù)名(參數(shù)列表) -> 返回類型{函數(shù)的執(zhí)行體}
// 調(diào)用swift函數(shù)時(shí)贴彼,在默認(rèn)情況下從第二個(gè)參數(shù)開(kāi)始需要寫函數(shù)名
// 如果調(diào)用函數(shù)的時(shí)候沒(méi)有給該函數(shù)參數(shù)可以給它賦個(gè)默認(rèn)值
//func sayHello(personName: String,alreadyGreeted: Bool = false) -> String {
// if alreadyGreeted {
// return "怎么又是你," + personName + "!"
// }
// else {
// return "你好拉宗," + personName + "!"
// }
//
//}
//
//// 調(diào)用函數(shù)
//// 函數(shù)名(參數(shù)值)
//print(sayHello("王大錘"))
//let str = sayHello("王大錘",alreadyGreeted: true)
//print(str)
//函數(shù)的可變參數(shù)列表(參數(shù)的個(gè)數(shù)是任意多個(gè))
//func sum(nums: Int...) ->Int {
// var total = 0
// for num in nums {
// total += num
// }
// return total
//}
//
//print(sum())
//print(sum(999))
//print(sum(1,2,3))
//
//可以使用元組(tuple)讓函數(shù)一次返回多條數(shù)據(jù)
//func minMax(array: [Int]) -> (min: Int, max: Int)? {
// if array.isEmpty{
// return nil
// }
// var currentMin = array[0]
// var currentMax = array[0]
//
//
// for value in array[1..<array.count] {
// if value < currentMin {
// currentMin = value
// }
// else if value > currentMax {
// currentMax = value
// }
// }
//
//
// return (currentMin, currentMax)
//}
//
//if let b = minMax([1,3,5,7,9,11,13,15,17]){ //處理可空類型的最好方法 若為空 函數(shù)就不會(huì)執(zhí)行
// print(b.min) // print(b.0)
// print(b.max) // print(b.1)
//}
//
//func swap(inout a: Int, inout _ b: Int) {
//// (a,b) = (b,a)
// let temp = x
// x = y
// y = temp
//}
//
//var x = 5 ,y = 10
////函數(shù)調(diào)用的傳參都是傳值
//swap(&x,&y)
//print("x = \(x)")
//print("y = \(y)")
func swap(var x: Int, var _ y: Int) {
(x,y) = (y,x)
}
var x = 5 ,y = 10
//函數(shù)調(diào)用的傳參都是傳值
swap(x,y)
print("x = \(x)")
print("y = \(y)")
//inout - 輸入輸出參數(shù)(不僅將數(shù)據(jù)傳入函數(shù)還要從函數(shù)中取出數(shù)據(jù))
//func createX(inout x: Int ) {
// x = 1000
//}
//
//var x = 1
//createX(&x)
//print(x)