1.多行文字的字符串:
之前我們可以通過使用 “\ n” 來使字符串換行比如:
let beforeString = "When you write a string that spans multiple \nlines make sure you start its content on a \nline all of its own, and end it with three \nquotes also on a line of their own. Multi-line strings also let you write "quote marks" \nfreely inside your strings, which is great!" //print(beforeString)
這種方式閱讀起來很不方便,看起來很不美觀踊谋,不能直觀的顯示它所要呈現(xiàn)給用戶展示的樣子州邢。
在swift4.0中,提供了專門的語法來顯示多行字符串褪子,從而告別轉(zhuǎn)義:
1)以三個(gè)雙引號(hào)作為開始的標(biāo)識(shí)。
2)以三個(gè)雙引號(hào)作為結(jié)束的標(biāo)識(shí)骗村。
3)不管開始標(biāo)識(shí)還是結(jié)束標(biāo)識(shí)嫌褪,都必須單獨(dú)占據(jù)一行
4)你定義的字符串就是開始標(biāo)識(shí)和結(jié)束標(biāo)識(shí)中間的樣子
let longString =
"""
When you write a string that spans multiple
lines make sure you start its content on a
line all of its own, and end it with three quotes also on a
line of their own. Multi-line strings also let you write "quote marks" freely inside your strings, which is great!
"""
print(longString)
2.改進(jìn)鍵值編碼的關(guān)鍵字
swift中如何使用的keyPath呢?
首先胚股,我們定義兩個(gè)結(jié)構(gòu)體:
struct Crew {
var age: String
var height: String
}
struct SuperMen {
var name: String
var age: Double
var height: Crew
func goToMaximumWarp() {
print("\(name) is now travelling at warp \(maxWarp)")
}
}
let janeway = Crew(name:"Kathryn Janeway",rank:"Captain")
let voyager = Starship(name: "Voyager", maxWarp: 9.975, captain: janeway) let enter = voyager.goToMaximumWarp enterWarp()
在swift中笼痛,我們可以給函數(shù)添加一個(gè)引用。比如琅拌,我們可以給goToMaximumWarp()方法添加一個(gè)叫做enter的引用缨伊,之后我們便可以使用enter來調(diào)用它。然而进宝,我們卻不能對(duì)屬性做同樣的操作刻坊。是的,我們不能給SuperMen的名字屬性添加一個(gè)引用党晋。
這個(gè)問題谭胚,可以通過使用keypath來解決:正如enter()一樣徐块,它們是未被調(diào)用的屬性引用。如果您現(xiàn)在調(diào)用引用灾而,則得到當(dāng)前值胡控,但如果稍后調(diào)用引用,則獲得最新值
旁趟。keyPath的語法格式為反斜杠:
let nameKeyPath = \SuperMen.name
let maxWarpKeyPath = \SuperMen.age
let captainName = \SuperMen.captain.height
print(voyager[keyPath: nameKeyPath]) //Voyager
voyager[keyPath: nameKeyPath] = "456" print(voyager.name) //456
voyager.goToMaximumWarp() //456 is now travelling at warp 9.975
enterWarp() //Voyager is now travelling at warp 9.975
let starshipName = voyager[keyPath: nameKeyPath]
let starshipMaxWarp = voyager[keyPath: maxWarpKeyPath]
let starshipCaptain = voyager[keyPath: captainName]
3.改進(jìn)了字典功能:
Swift4.0讓詞典的功能更強(qiáng)大昼激。
在Swift3.0中,Dictionary的過濾函數(shù)會(huì)返回一個(gè)包含key / value元組的數(shù)組锡搜。比如
:
let names = ["小明": 24_256_800, "小強(qiáng)": 23_500_000, "笑話": 21_516_000, "Seoul": 9_995_000];
let massiveName = names.filter { $0.value > 10_000_000 }
在Swift3.0中橙困,你不能通過massiveName [ “小明”]來獲取對(duì)應(yīng)的值。
因?yàn)閙assiveName不是一個(gè)字典類型余爆。只能通過massiveName [0]纷宇。價(jià)值來獲取。
但在Swift4.0中蛾方,massiveName是字典類型像捶,使用massiveName [ “小明”]獲取值完全沒有問題。
4.String又變成了集合類型:
這意味著桩砰,你可以做字符串倒置拓春,循環(huán)獲取每個(gè)字符,map亚隅,flatMap()等操作硼莽。
比如:
let quote = "It is a truth universally acknowledged that new Swift versions bring new features."
let reversed = quote.reversed()
for letter in quote {
print(letter)
}
另外,Swift4.0中煮纵,引入類似于python中字符串的一些操作懂鸵。在省略起始位置或者結(jié)束位置的情況下,可以自動(dòng)推斷集合的起始位置或者結(jié)束位置行疏。
let person = ["name", "age", "heght", "hand", "foot"]
let big = person[..<3]
let small = person[3...]
print(big) //["name", "age", "height"]
print(small) //["hand", "foot"]