Skip to content

Instantly share code, notes, and snippets.

@holleB
Last active January 18, 2021 17:23
Show Gist options
  • Save holleB/7b5875b6881a355f5b73feab8564b8a9 to your computer and use it in GitHub Desktop.
Save holleB/7b5875b6881a355f5b73feab8564b8a9 to your computer and use it in GitHub Desktop.
var mydict = ["a" : [1, 2, 3]]
mydict["a"]!.append(4)
// compiles and seems to work
var mydict2 : [String : Any] = ["a" : [1, 2, 3]]
mydict2["a"]!.append(4) // does not work. thats expected
(mydict2["a"] as! [Int]).append(4)
// does not work, unexpected for me. stackoverflow says that's because forcecasting value types
// creates a copy which is immutable
// how do I append to the array?
var el = mydict2["a"] as! [Int]
el.append(4)
mydict2["a"] = el
// that's seems overblown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment