swift4.0新特性

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è)雙引號作為開始的標(biāo)識。
2)以三個(gè)雙引號作為結(jié)束的標(biāo)識。
3)不管開始標(biāo)識還是結(jié)束標(biāo)識,都必須單獨(dú)占據(jù)一行
4)你定義的字符串就是開始標(biāo)識和結(jié)束標(biāo)識中間的樣子

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)用它。然而,我們卻不能對屬性做同樣的操作。是的,我們不能給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 [ “小明”]來獲取對應(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"]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容