RookieNeoWhats-new-in-swift-4 (下)

Dictionary and Set enhancements

关于字典的改进都是很直观的.

let names = ["Cagney", "Lacey", "Bensen"]
let dict = Dictionary(uniqueKeysWithValues: zip(1..., names))
//print [2: "Lacey", 3: "Bensen", 1: "Cagney"]
dict[2]
print Lacey
//
let contacts = ["Julia", "Susan", "John", "Alice", "Alex"]
let grouped = Dictionary(grouping: contacts, by: { $0.first! })
grouped
//print ["J": ["Julia", "John"], "S": ["Susan"], "A": ["Alice", "Alex"]]

很多时候服务器返回的JSON并不会给你分组,但是很有需求是需要在list中分组的,这个方法是很好的.