Swift:通过引用传递数组? [英] Swift: Pass array by reference?
问题描述
我想通过引用将我的 Swift Array
account.chats
传递给 chatsViewController.chats
(这样当我添加聊天到 account.chats
,chatsViewController.chats
仍然指向 account.chats
).即,当 account.chats
的长度发生变化时,我不希望 Swift 将两个数组分开.
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.
推荐答案
Swift 中的结构是按值传递的,但您可以使用 inout
修饰符来修改您的数组(请参阅下面的答案).类是通过引用传递的.Array
和 Dictionary
在 Swift 中被实现为结构体.
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.
这篇关于Swift:通过引用传递数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!