gpt4 book ai didi

ios - 来自多个循环的对象数组

转载 作者:行者123 更新时间:2023-11-30 11:41:31 26 4
gpt4 key购买 nike

我想最终得到一组自定义对象。当我有多个数组需要迭代以创建自定义对象时,我很难找到解决此问题的方法。我有这样的东西:

class CustomObject {
var name = ""
var place = ""
var price = ""
}

var nameArray = [“name1”,"name2","name3"]
var placeArray = [“place1”,"place2","place3"]
var priceArray [“price1”,"price2","price3"]
var customArray [CustomObject]()
createObject(name : nameArray, place : placeArray, price : priceArray)

func createObject(name : [String], place : [String], price : [String]) {
let instance = CustomObject()
for names in name {
instance.name = names
}
for places in place {
instance.place = places
}
for prices in price {
instance.price = prices
customArray.append(instance)
}
}

我想最终得到一组自定义“实例”。现在我的代码为每个属性提供每个数组的最后一个索引。

最佳答案

你可以尝试,我也认为所有数组都有相同的计数

 var customArray = [CustomObject]()

for i in 0..<nameArray.count {

let instance = CustomObject()

instance.name = nameArray[i]

instance.place = placeArray[i]

instance.price = priceArray[i]

customArray.append(instance)
}

关于ios - 来自多个循环的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241763/

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