Swift:通过引用传递数组? [英] Swift: Pass array by reference?

查看:37
本文介绍了Swift:通过引用传递数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过引用将我的 Swift Array account.chats 传递给 chatsViewController.chats (这样当我添加聊天到 account.chatschatsViewController.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 修饰符来修改您的数组(请参阅下面的答案).类是通过引用传递的.ArrayDictionary 在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆