Search code examples
arraysswiftpass-by-reference

Swift: Pass array by reference?


I want to pass my Swift Array account.chats to chatsViewController.chats by reference (so that when I add a chat to account.chats, chatsViewController.chats still points to account.chats). I.e., I don't want Swift to separate the two arrays when the length of account.chats changes.


Solution

  • Structs in Swift are passed by value, but you can use the inout modifier to modify your array (see answers below). Classes are passed by reference. Array and Dictionary in Swift are implemented as structs.