gpt4 book ai didi

ios - 如何像stack swift 5一样在数组中一个接一个地添加/插入元素

转载 作者:行者123 更新时间:2023-11-28 05:37:27 26 4
gpt4 key购买 nike

我从 api 响应中获取字符串并存储在一个数组中。

     for customParams in custom_attributes! {
if(customParams["attribute_code"] as! String == "small_image")
{
print(customParams["value"])
var myString = customParams["value"] as! String

//want to add this myString in array(myArray) one after another
var myArray :[String] = []
print(myArray)
}
}

输出:

myArray = ["myString1","myString2"......]

伙计们,我该怎么做?我还是 swift 的新手...提前致谢!

"id": 6,
"products": {
"items": [{
"status": 1,
"custom_attributes": [
{
{
"attribute_code": "small_image",
"value": "small_image"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}]},
{
"status": 1,
"custom_attributes": [
{
{
"attribute_code": "small_image",
"value": "small_image"
},
{
"attribute_code": "meta_keyword",
"value": "meta_keyword"
}
]}]}}]

想要获取 customParams["attribute_code"] == small_image

最佳答案

您需要先遍历 items 数组,因为它在 json 中,还在 for 循环外声明 myArray 并开始在 myArray 内附加 myString,如下所示:

var myArray :[String] = []
for custom_attributes! in items { // items is the json items array
for customParams in custom_attributes! {
if(customParams["attribute_code"] as! String == "small_image")
{
print(customParams["value"])
var myString = customParams["value"] as! String
myArray.append(myString)

}
}
}

关于ios - 如何像stack swift 5一样在数组中一个接一个地添加/插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58198403/

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