Skip to content

Instantly share code, notes, and snippets.

@takasek
Created November 15, 2017 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takasek/6d2eca7dcfd447f0d01870805c25362d to your computer and use it in GitHub Desktop.
Save takasek/6d2eca7dcfd447f0d01870805c25362d to your computer and use it in GitHub Desktop.
ArrayやDictionaryの中身のstructを取り出して、その場でmutatingな処理をしたとき、ちゃんとarr/dict自体も書き換わってくれる。へー。 #CodePiece
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