我正在使用Swift创建一个需要与此格式匹配的数组:
let items = [["1", "red", "33", "canada"], ["2", "blue", "66", "usa"]]
在我的代码中,我查询一个数据库,并返回包含相关信息的多行:
let items = [id+" - "+colour+" - "+number+" - "+home_location]
我使用循环.append
数组,但格式却改为这样:
["1 - red - 33 - canada", "2 - blue - 66 - usa"]
我需要做什么来创建所需的数组结构?
答案 0 :(得分:2)
对于数据库的每一行,而不是
let items = [id+" - "+colour+" - "+number+" - "+home_location]
说
let items = [id, colour, number, home_location]
现在将其附加到var
的{{1}}数组中。
答案 1 :(得分:0)
为您的数据创建模型
class ModelClass {
var id = ""
var colour = ""
var number = ""
var home_location = ""
}
然后像这样创建模型类的对象
let objmodel : ModelClass = ModelClass()
objmodel.id = "1"
objmodel.colour = "red"
objmodel.number = "33"
objmodel.home_location = "canada"
然后创建您的主数组并将此模型对象附加到模型数组
var arrData = [ModelClass]()
arrData.append(objmodel)