Created
November 15, 2017 11:35
-
-
Save takasek/6d2eca7dcfd447f0d01870805c25362d to your computer and use it in GitHub Desktop.
ArrayやDictionaryの中身のstructを取り出して、その場でmutatingな処理をしたとき、ちゃんとarr/dict自体も書き換わってくれる。へー。 #CodePiece
This file contains hidden or 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
struct Hoge { | |
var int: Int | |
} | |
// ----------------------- | |
// Array 編 | |
var arr: [Hoge] = [ | |
Hoge(int: 1), | |
Hoge(int: 2), | |
Hoge(int: 3), | |
] | |
arr[2].int = 5 | |
arr // [{int 1}, {int 2}, {int 5}] | |
// ----------------------- | |
// Dictionary 編 | |
var dict: [String: Hoge] = [:] | |
dict["a"] = Hoge(int: 1) | |
dict["b"] = Hoge(int: 2) | |
dict["c"] = Hoge(int: 3) | |
dict["a"]?.int = 6 | |
dict // ["b": {int 2}, "a": {int 6}, "c": {int 3}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment