简体   繁体   English

Swift:如何在数组中使用变量?

[英]Swift: How do you use a variable in an array?

I am using a pod called iOSDropDown to display a dropdown selection menu for a textfield.我正在使用一个名为 iOSDropDown 的 pod 来显示文本字段的下拉选择菜单。 I am getting a list of data from php to populate that selection menu that Im storing in a variable.我从 php 获取数据列表,以填充我存储在变量中的选择菜单。

The PHP data passed stored in a swift variable looks like this when printed -> "option01","option02","option03" ... and so on.存储在 swift 变量中的 PHP 数据在打印时如下所示 -> "option01","option02","option03" ... 等等。 This is dynamic data that will change that is why I am retrieving from PHP/MYSQL Database instead of just manually typing in the options in the Swift array.这是会改变的动态数据,这就是为什么我从 PHP/MYSQL 数据库中检索而不是仅仅手动输入 Swift 数组中的选项。

Below is my code.下面是我的代码。 What I am trying to do is use the "dropdownData" variable that holds the options for the array.我想做的是使用保存数组选项的“dropdownData”变量。 Each option should be in its own row and separately selectable.每个选项都应该在自己的行中并且可以单独选择。 What I am getting is one option, one string of coding with all my options as shown in the picture below.How would I use the dropdownData variable to display options instead of one string, one option?我得到的是一个选项,一个带有我所有选项的编码字符串,如下图所示。我将如何使用 dropdownData 变量来显示选项而不是一个字符串,一个选项?

dropdownData = "option01","option02","option03"... etc. ALL OPTIONS STORED IN THIS ONE ARRAY dropdownData = "option01","option02","option03"... 等等所有选项都存储在这个数组中

let dropdownData : String = (dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as! String

. .

cell.nameField.optionArray = [dropdownData]

Image图片

In the image above there should be no comma after styrofoam cooler... the next product should be the next option displaying under the styrofoam cooler and separately selectable.在上图中,泡沫塑料冷却器后面应该没有逗号……下一个产品应该是泡沫塑料冷却器下方显示的下一个选项,并且可以单独选择。

Seems like dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") returns a String where names are comma separated,似乎dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData")返回一个字符串,其中名称以逗号分隔,

so所以

    if let nameString = dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as? String {
        let namesArray = nameString.components(separatedBy: ",")
        cell.nameField.optionArray = namesArray
    }

That should do那应该做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM