Skip to content

Instantly share code, notes, and snippets.

@amorphobia
Created May 31, 2019 02:18
Show Gist options
  • Save amorphobia/5a450a0ab616b88cbe6570e4d365be0c to your computer and use it in GitHub Desktop.
Save amorphobia/5a450a0ab616b88cbe6570e4d365be0c to your computer and use it in GitHub Desktop.
Swift: Append an element to an array which is a value of a dictionary, creating a new array if needed
var dict = [1: [11, 13, 15], 2: [23, 24]]
dict[3, default: []].append(39)
@amorphobia
Copy link
Author

Note

Do not use this subscript to modify dictionary values if the dictionary’s Value type is a class. In that case, the default value and key are not written back to the dictionary after an operation.

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment