arrays - 如何分配一个变量来引用一个数组?

标签 arrays swift xcode swift3 xcode8

  • 免责声明:我几乎是 Swift 的新手,所以请做好畏缩的准备。

我设置了一系列数组,当用户点击一个按钮时,我希望调用其中一个数组,以便我可以在函数中使用它。它们以 thisArray 命名,然后是一个数字,即:thisArray1、thisArray2 等。

我找不到使这项工作可行的方法,最接近我想要的如下所示,但正如你们都知道的那样,这绝对行不通。

var currentArray = "thisArray"+ selectedArrayNumber

这样做的结果是使用如下变量:

button1.setTitle(currentArray[1], for .normal)

如果你们中的任何人能阐明这种情况并告诉我我犯了多么严重的错误,我们将不胜感激。

最佳答案

创建一个数组数组,然后使用 selectedArrayNumber 检索它。

let arrays = [thisArray1, thisArray2, etc]
let currentArray = arrays[selectedArrayNumber]

关于arrays - 如何分配一个变量来引用一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196704/

相关文章:

javascript - 在数组上设置状态在 native react 中不起作用?

ios - UITableView 不更新使用 Kingfisher 下载的图像

objective-c - Xcode 找不到 ProductModuleName-Swift.h

ios - 在 UIViewController 之外修改 UIView 的最佳方法

iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom

ios - Swift:如何使用 touchesBegan/Moved 管理多个可拖动的 uiimageview

javascript - 尝试根据返回值过滤对象数组

javascript - 将另一个对象推送到数组中

python - 如何按列对二维列表进行排序

ios - 测试目标上出现 "ld: framework not found GGLAnalytics for architecture x86_64"错误