gpt4 book ai didi

swift 3 : Select an Array based on a dictionary

转载 作者:行者123 更新时间:2023-11-28 15:53:57 25 4
gpt4 key购买 nike

我有一个弹出 View Controller ,其中包含一个带有选项列表的 TableView ,类似于下拉选择列表。该 TableView 是动态填充的,具体取决于用户在父 View Controller 上点击的“选择”按钮。我为各种选项集构建了几个数组来填充 TVC。如何使用 fieldIndex 字典自动设置要用作弹出窗口的 TVC 数据源的数组?

class MyWidget : NSObject {

var fieldIndex : [Int:String] = [
0 : "make",
1 : "model",
2 : "year"
]

var make = String()
var model = String()
var year = String()
}

var makes = ["Acme", "Contoso", "Tradewinds"]
var models = ["Model1", "Model2", "Model3"]
var years = ["2015", "2016", "2017"]

Parent View Controller

最佳答案

您可以简单地创建Int: [String] 类型的字典

class MyWidget : NSObject { 
// empty fieldData dictionary of type Int: [String]
var fieldData = [Int: [String]]()

}

//这样添加数据

fieldData[0] = ["Acme", "Contoso", "Tradewinds"]
fieldData[1] = ["Model1", "Model2", "Model3"]
fieldData[2] = ["2015", "2016", "2017"]

//这样获取数据

let makeArray = fieldData[0] 

关于 swift 3 : Select an Array based on a dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com