difference
函數用於任意元素實現 Equaltable 、且有序集合。數組
下面是該函數的示例代碼:微信
let bird = "bird"
let bear = "bear"
// step1
let diff = bird.difference(from: bear) //兩個字符串不一樣的部分: i d e a
// step2
let newBird = bear.applying(diff) // bird
複製代碼
step1 經過 difference
函數得到了 bird 和 bear 不一樣的部分 diff,step2 經過調用 applying
函數將 diff 應用到 bear 上來獲得 newBird。markdown
由於 difference
函數能夠應用於任意有序集合,因此也能夠在數組中使用:app
let arr1 = [1, 2, 3]
let arr2 = [2, 3, 4]
let diff = arr1.difference(from: arr2) // 兩個數組不一樣的部分:[1, 4]
let newArr1 = arr2.applying(diff)
print(newArr1) // [1, 2, 3]
複製代碼
由於 difference
函數返回的結果自己也是一個集合,因此咱們也能夠使用 for-in 來迭代它的元素:ide
for change in diff {
switch change {
case .remove(let offset, let element, _):
print("offset = \(offset), element = \(element)") // offset = 2, element = 4
case .insert(let offset, let element, _):
print("offset = \(offset), element = \(element)") // offset = 0, element = 1
}
}
複製代碼
在 Swift 5.1,官方提供了數據壓縮函數,但該函數僅限於 NSData 使用。函數
示例代碼:測試
let str = "Swift"
let data = str.data(using: .utf8)
if let d = data {
let d1 = d as NSData
let res = try d1.compressed(using: .lzfse)
print(res)
}
複製代碼
CompressionAlgorithm 爲枚舉,提供四種 case:lz四、lzfse、lzma、zlib。優化
UnitDuration 新增如下類型:spa
UnitFrequency 新增 framesPerSecond,可用來測試 FPS。code
UnitInformationStorage 新增如下類型:
新增 RelativeDateTimeFormatter 用來計算兩個日期之間的間隔,支持本地化。這個更新很實用,能夠很方便的計算各類相似於微信聊天的時間標籤。
let oldDateStr = "2021-01-05"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let oldDate = dateFormatter.date(from: oldDateStr)!
let nowDate = Date()
let formatter = RelativeDateTimeFormatter()
formatter.locale = Locale(identifier: "zh_CN")
let dateString = formatter.localizedString(for: oldDate, relativeTo: nowDate)
print(dateString) // 1周前
複製代碼
用於本地化顯示數組轉成的字符串:
let str = ["🤣", "🐖", "🦄"]
let formatter = ListFormatter()
formatter.locale = Locale(identifier: "zh_CN")
let zhString = formatter.string(for: str)
print(zhString) // "🤣、🐖和🦄"
formatter.locale = Locale(identifier: "en_US")
let enString = formatter.string(for: str)
print(enString) // "🤣, 🐖, and 🦄"
複製代碼
經過 itemFormatter 來設置 item 的格式。
func stringToDate(str: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from:str)
}
let dateStr = ["2020-01-09", "2020-02-05", "2020-04-21"]
let dates = dateStr.compactMap { stringToDate(str: $0) }
let listFormatter = ListFormatter()
let dateFromatter = DateFormatter()
dateFromatter.dateFormat = "yyyy-MM-dd"
listFormatter.itemFormatter = dateFromatter
let str = listFormatter.string(for: dates) // "2020-01-09, 2020-02-05, and 2020-04-21"
// 經過設置 locale 來進行本地化輸出
listFormatter.itemFormatter = dateFromatter
listFormatter.locale = Locale(identifier: "es_ES")
let str = listFormatter.string(for: dates) // "2020-01-09, 2020-02-05 y 2020-04-21"
複製代碼
新增 addBarrierBlock
。當全部 task 執行完以後,纔會執行 addBarrierBlock
的 task。
let queue = OperationQueue()
queue.addOperation {
print("task1")
}
queue.addOperation {
print("task2")
}
queue.addOperation {
print("task3")
}
queue.addOperation {
print("task4")
}
queue.addBarrierBlock {
print("complete")
}
// task1 task2 task3 task4 complete
複製代碼
經過設置 totalUnitCount 來進行進度檢測:
queue.progress.totalUnitCount = 3
複製代碼
優化了 Scanner 的使用方式。
let scanner = Scanner(string: "")
// Swift 4
var nameNSString: NSString?
if scanner.scanUpToCharacters(from: .newlines, into: &nameNSString) {
let name = nameNSString! as String
}
// Swift 5.1
let nameString = scanner.scanUpToCharacters(from: .newlines)
複製代碼