Last active
August 23, 2018 23:23
-
-
Save hanawat/c42f1d2106380d1636c1e5b5c1ebb4d6 to your computer and use it in GitHub Desktop.
Swift4.0でDictionaryが大幅にパワーアップした ref: https://qiita.com/hanawat/items/341a6f7843f4e19780b5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let touples = [("Apple", 1), ("Orange", 2), ("Grape", 3)] | |
// ["Grape": 3, "Orange": 2, "Apple": 1] | |
let dictionary = Dictionary(uniqueKeysWithValues: touples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Orange", "Grape"] | |
let count = [1, 2, 3] | |
let ziped = zip(names, count) | |
// ["Grape": 3, "Orange": 2, "Apple": 1] | |
let dictionary = Dictionary(uniqueKeysWithValues: ziped) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Orange", "Apple", "Grape"] | |
var dictionary = [String: Int]() | |
names.forEach { dictionary[$0, default: 0] += 1 } | |
// ["Grape": 1, "Orange": 1, "Apple": 2] | |
dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Grape", "Apricot", "Guava"] | |
var initialGroup = [Character: [String]]() | |
names.sorted().forEach { name in | |
let character = name.characters.first ?? "#" | |
var names = initialGroup[character] ?? [] | |
names.append(name) | |
initialGroup[character] = names | |
} | |
// ["G": ["Grape", "Guava"], "A": ["Apple", "Apricot"]] | |
initialGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Grape", "Apricot", "Guava"] | |
// ["G": ["Grape", "Guava"], "A": ["Apple", "Apricot"]] | |
let initialGroup = Dictionary(grouping: names) { $0.first ?? "#" } | |
// [5: ["Apple", "Grape", "Guava"], 7: ["Apricot"]] | |
let countGroup = Dictionary(grouping: names) { $0.count } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dictionary = [String: Int]() | |
print(dictionary.capacity) // 0 | |
dictionary.reserveCapacity(4) | |
print(dictionary.capacity) // 6 | |
dictionary.reserveCapacity(7) | |
print(dictionary.capacity) // 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Apple", "Grape"] | |
let count = [1, 2, 3] | |
let ziped = zip(names, count) | |
// Execution was interrupted, reason: EXC_BAD_INSTRUCTION | |
let dictionary = Dictionary(uniqueKeysWithValues: ziped) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Apple", "Grape"] | |
let count = [1, 2, 3] | |
let ziped = zip(names, count) | |
// ["Grape": 3, "Apple": 3] | |
let merged = Dictionary(ziped, uniquingKeysWith: +) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictionary = ["Grape": 3, "Orange": 2, "Apple": 1] | |
// ["Grape": 6, "Orange": 2, "Apple": 4] | |
let merged = dictionary.merging(ziped, uniquingKeysWith: +) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictionary = ["Grape": 3, "Orange": 2, "Apple": 1] | |
// Swift3: [(key: "Grape", value: 3), (key: "Orange", value: 2)] | |
// Swift4: ["Grape": 3, "Orange": 2] | |
let filterd = dictionary.filter { $0.value > 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictionary = ["Grape": 3, "Orange": 2, "Apple": 1] | |
var mapped = [String: Int]() | |
dictionary.forEach { touple in | |
mapped[touple.key] = touple.value * 2 | |
} | |
// ["Grape": 6, "Orange": 4, "Apple": 2] | |
mapped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictionary = ["Grape": 3, "Orange": 2, "Apple": 1] | |
// ["Grape": 6, "Orange": 4, "Apple": 2] | |
let mapped = dictionary.mapValues { $0 * 2 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictionary = ["Grape": 3, "Orange": 2, "Apple": 1] | |
// 4 | |
let potato = dictionary["Potato", default: 4] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let names = ["Apple", "Orange", "Apple", "Grape"] | |
var dictionary = [String: Int]() | |
names.forEach { name in | |
// Binary operator '+=' cannot be applied to operands of type 'Int?' and 'Int' | |
// dictionary[name] += 1 | |
let value = dictionary[name] ?? 0 | |
dictionary[name] = value + 1 | |
} | |
// ["Grape": 1, "Orange": 1, "Apple": 2] | |
dictionary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment