概述階段
MVVM架構(gòu)模式
MVVM多了個(gè)ViewModel層。
數(shù)據(jù)請(qǐng)求完成后,先進(jìn)入到ViewModel層,該轉(zhuǎn)譯轉(zhuǎn)譯,該封裝封裝,然后在由接口返回。
View層負(fù)責(zé)把組合后的model的各個(gè)屬性與View中的各個(gè)組件綁定,比如A組件綁定model的a屬性。
與MVC最大的不同:
1.ViewController不在具有數(shù)據(jù)處理能力,僅僅負(fù)責(zé)數(shù)據(jù)與控件的綁定。
2.ViewModel層主要處理數(shù)據(jù)、處理一些小的業(yè)務(wù)邏輯。功能單一化,專業(yè)換。
3.如果需求不涉及UI位置的變化,僅數(shù)據(jù)內(nèi)容更改,往往只需要去修改ViewModel層就好了,更為便捷
Swift知識(shí)點(diǎn):
1.String能正確識(shí)別Unicode編碼編碼
2.字符串“33.0”無法強(qiáng)轉(zhuǎn)為int類型
3.字符串轉(zhuǎn)數(shù)字,結(jié)果為可選值(帶?),Swift中不像OC會(huì)自動(dòng)賦0,轉(zhuǎn)換失敗就是nil
代碼階段:
String和Character、String識(shí)別Unicode編碼
//定義Character數(shù)組
let catCharacters: [Character] = ["C", "a", "t", "!", "??"]
// 字符數(shù)組轉(zhuǎn)字符串
let catString = String(catCharacters)
//在字符串中存入U(xiǎn)nicode編碼將被自動(dòng)識(shí)別
//由于指定類型,所以為Character
let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
//不指定類型的話默認(rèn)就是String
let regionalIndicatorForUS2 = "\u{1F1FA}\u{1F1F8}"
//????
print(regionalIndicatorForUS)
//???? 1 Unicode編碼長(zhǎng)度被正確識(shí)別
print(regionalIndicatorForUS2,regionalIndicatorForUS2.characters.count)
String的遍歷、截取、轉(zhuǎn)編碼
//str的遍歷方式1
for character in "Dog!??".characters {
print(character)
}
//str的遍歷方式2
let str = "Dog!??"
for index in str.characters.indices {
//這里的index并不是一個(gè)真真的int,不可直接使用
print("\(str[index])")
}
// 截取字符串
let baseStr = "Guten Tag!"
//截取首個(gè)字符
print(baseStr[baseStr.startIndex])
//截取前三個(gè)字符(string中發(fā)生截取常需要用到Index結(jié)構(gòu)體)
print(baseStr.substring(to: baseStr.index(baseStr.startIndex, offsetBy: 3)))
// 截取一段(起始位置為2,結(jié)束位置為endIndex-1)
print(baseStr[baseStr.index(baseStr.startIndex, offsetBy: 2)..<baseStr.index(baseStr.endIndex, offsetBy: -1)])
var welcome = "hello,World"
// 在指定位置插入一個(gè)字符
welcome.insert("!", at: welcome.endIndex)
//輸出結(jié)果:hello,World!
print(welcome)
//在指定位置之前插入一個(gè)字符串(依然涉及index結(jié)構(gòu)體)
welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex))
//輸出結(jié)果:hello,World there!
print(welcome)
//移除最后一個(gè)字符(remove僅能移除一個(gè)字符)
welcome.remove(at: welcome.index(before: welcome.endIndex))
//輸出結(jié)果:hello,World there
print(welcome)
//移除最后6個(gè)字符
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
//hello,World
print(welcome)
//檢查前綴和后綴(前綴:hasPrefix 后綴:hasSuffix)
if welcome.hasSuffix("World"){
}
//把每個(gè)字符轉(zhuǎn)編碼
for codeUnit in welcome.utf16 {
print("\(codeUnit) ", terminator: "")
}
余數(shù)運(yùn)算函數(shù)(truncatingRemainder)
//取余數(shù)運(yùn)算函數(shù)(truncatingRemainder)
print(41.truncatingRemainder(dividingBy: 4))
//甚至可以取浮點(diǎn)數(shù)的余數(shù)
let z = CGFloat(7.1).truncatingRemainder(dividingBy: 1.2)
print(z)
Self 和 擴(kuò)展
Self 用于某個(gè)對(duì)象所歸屬的類,無法確定其具體類型
//需求:給所有數(shù)字類型,擴(kuò)展一個(gè)平方的函數(shù),返回自己的操作
// 定義個(gè)協(xié)議
protocol NumberProtocol{
}
// 擴(kuò)展實(shí)現(xiàn)協(xié)議
extension Int:NumberProtocol{
}
extension Double:NumberProtocol{
}
extension Float:NumberProtocol{
}
// 給協(xié)議擴(kuò)展方法
extension NumberProtocol{
// 我們不確定返回的Self 到底是什么類型
func squareValue()-> Self{
if self is Int{
let n = self as! Int
return n * n as! Self
}
if self is Double{
let n = self as! Double
return n * n as! Self
}
if self is Float{
let n = self as! Float
return n * n as! Self
}
//走到這里的話大部分情況下會(huì)報(bào)錯(cuò),說明輸入的類型不正確
return 0 as! Self;
}
}
//9.0601
print(Float(3.01).squareValue())
//11.8336
print(3.44.squareValue())
//9
print(3.squareValue())