我正在使用 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"]
我需要做什么来创建所需的数组结构?
最佳答案
对于数据库的每一行,而不是
let items = [id+" - "+colour+" - "+number+" - "+home_location]
说
let items = [id, colour, number, home_location]
现在将其附加到 [[String]]
的 var
数组中。
关于arrays - 在 Swift 中创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51530722/