學(xué)習(xí)資料來源Swift 圖書展示項目開發(fā)實戰(zhàn),閱讀了一些其它文章贞瞒,案例還是講的通熟易懂,記錄一下棕洋,方便后期回憶。
泛型
var num1 = "123", num2 = "str:"
func swapValue<T>(_ num1:inout T, _ num2:inout T){
(num1,num2) = (num2,num1)
}
swapValue(&num1, &num2)
num1
num2
守衛(wèi)
func checkup(person:[String:String]){
guard let id = person["id"] else {
print ("沒有身份證不許進(jìn)考場")
return
}
guard let examNumber = person["examNumber"] else {
print ("沒有準(zhǔn)考證掰盘,不許進(jìn)考場")
return
}
print ("身份證號\(id),準(zhǔn)考證\(examNumber)")
}
//checkup(person: ["id":"111","examNumber":"222"])
checkup(person: ["id":"111"])
屬性觀察
Swift - 屬性觀察器 Property Observer
let MaxValue = 999
let Minvalue = -999
var number:Int = 0 {
willSet {
print ("將從\(number)變成\(newValue)")
}
didSet{
if number > MaxValue {
number = MaxValue
} else if number < Minvalue {
number = Minvalue
}
print ("已經(jīng)從\(oldValue)變?yōu)閈(number)")
}
}
number = 1000
number
擴(kuò)展
iOS學(xué)習(xí)筆記46-Swift(六)擴(kuò)展
extension Int {
func times(closure : (()->())?){
if self >= 0 {
for _ in 0..<self{
closure?()
}
}
}
}
10.times{print ("hello")}
協(xié)議擴(kuò)展
extension CustomStringConvertible {
var upperdescription:String {
return self.description.uppercased()
}
}
["key":"value"].description
["key":"value"].upperdescription