參考
Raw strings
SE-0200增加了 # 符號,使得寫字符串更加簡單。
-
在字符串中包含 " 時不必再加 \
//before let rain = "The \"rain\" in \"Spain\" falls mainly on the Spaniards." //after let rain = #"The "rain" in "Spain" falls mainly on the Spaniards."# -
包含 \ 反斜杠也不需要再加轉義符
// before let keypaths = "Swift keypaths such as \\Person.name hold uninvoked references to properties." // after let keypaths = #"Swift keypaths such as \Person.name hold uninvoked references to properties."# 由于反斜杠作為字符串中的字符,所以在插入值的時候需要在后面再加個 #
//before
let answer = 42
let dontpanic = "The answer to life, the universe, and everything is \(answer)"
// after
let answer = 42
let dontpanic = #"The answer to life, the universe, and everything is \#(answer)"#
-
當字符串包含 # 時, 前后應用 ## 包裹字符串
let str = ##"My dog said "woof"#gooddog"## -
用 #""" 開頭 """#結尾 來表示多行字符串
let multiline = #""" The answer to life, the universe, and everything is \#(answer). """# -
由于不用反斜杠轉義 使得正則表達式更加簡潔明了
//before let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+" //after let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#
Handling future enum cases
SE-0192 在枚舉新增加一個 @unknown 修飾 default 分支,這樣使得將來 enum 再增加一個 case 的時候,編譯器會在該枚舉的switch 代碼里生成一個警告
比如
enum PasswordError: Error {
case short
case obvious
case simple
}
func showOld(error: PasswordError){
switch error {
case .short:
print("Your password was too short.")
case .obvious:
print("Your password was too obvious.")
default:
print("Your password was too simple.")
}
}
上面代碼假如我們再加個 case old,執(zhí)行代碼時它會自動進入到default分支,可這并不是我們想要的結果,因為這個密碼是一個舊密碼而不是密碼太簡單,這時候可以用 @unknown,如下
enum PasswordError: Error {
case short
case obvious
case simple
case old
}
func showNew(error: PasswordError) {
switch error {
case .short:
print("Your password was too short.")
case .obvious:
print("Your password was too obvious.")
@unknown default:
print("Your password wasn't suitable.")
}
}
這時會產生一個警告 ,因為新增了case old ,switch沒有明確地處理每一個分支。
Checking for integer muliples
SE-0225 integers 新增加了一個 isMultiple(of:) 方法
來檢測一個數(shù)是否是另一個數(shù)的倍數(shù)
let rowNumber = 4
/*相當于 if rowNumber % 2 == 0
改成方法調用更易懂,更方便調用(有代碼補全)
*/
if rowNumber.isMultiple(of:2) {
print("Even")
} else {
print("Odd")
}
Counting matching items in a sequence
SE-0220
Sequence 新增加了一個方法 count(where:) 相當于 filter() + count 的結果,但是它更加簡潔一步到位
//before
let scores = [100,80,85]
let passCount = scores.filter{($0 >= 85)}.count
//after 避免生成一個數(shù)組
let scores = [100,80,85]
let passCount = scores.count { $0 >= 85}
這個方法適用于遵循了 Sequence 的所有類型,所以也可以用在 集合 和 字典里。
Transforming and unwrapping dictionary values with compactMapValues()
SE-0218 為字典新增了一個方法 compactMapValues(),正如數(shù)組的compactMap函數(shù)一樣,可以過濾nil,類型轉換
let times = [
"Hudson": "38",
"Clarke": "42",
"Robinson": "35",
"Hartis": "DNF"
]
//通過compactMapValues 將值轉換成integer類型,并且將DNF過濾掉
let finishers1 = times.compactMapValues { Int($0) }
//也可以這樣寫
let finishers2 = times.compactMapValues(Int.init)
// 過濾nil
let people = [
"Paul": 38,
"Sophie": 8,
"Charlotte": 5,
"William": nil
]
let knownAges = people.compactMapValues { $0 }
結尾
Swift的每次更新都使得寫起代碼越來越簡單高效。
附上一個網站 What's new in Swift