gpt4 book ai didi

ios - 将循环中的对象从字典添加到数组

转载 作者:行者123 更新时间:2023-11-28 16:02:40 27 4
gpt4 key购买 nike

我想将字典中的对象添加到数组

我从 JSON 获取信息,我想用两个键和两个值在循环中添加这些信息:"name": value1, "imageURL": value2

我的循环中有 10 个项目,所以我尝试了这段代码,但是缺少 []:

let urlString = "https://api.unsplash.com/photos/?client_id=71ad401443f49f22556bb6a31c09d62429323491356d2e829b23f8958fd108c4"
let url = URL(string: urlString)!
let urlRequest = URLRequest(url: url)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)

var arr = [String]()
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
// do stuff with response, data & error here
if let statusesArray = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String: Any]] {
for item in statusesArray! {
let photos = item["urls"] as? [String: Any]
let photo = photos?["small"] as? String
var myDictionary = [
"name": "test",
"imageURL": photo]
for (key, value) in myDictionary {
arr.append("\(key): \(value)")
}
}
print(arr)
}
})

这给了我这个:

["imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test", "imageURL: https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name: test"]

现在,我想要这个(在循环中的每个对象上添加 []):

[["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"], ["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"], ["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"], ["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"], ["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"], ["imageURL": "https://images.unsplash.com/photo-1479859752262-6f02b79ad6ad?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=a613971b6da0b305154e37bbe01ed833", "name": "test"]]

我该如何解决这个问题?

最佳答案

更改这部分代码

var myDictionary = [
"name": "test",
"imageURL": photo]
for (key, value) in myDictionary {
arr.append("\(key): \(value)")
}

对此

var myDictionary = [
"name": "test",
"imageURL": photo]
arr.append(myDictionary)

您将字典添加到一个数组中,这样您就有了一个充满字典的数组。每个字典都是您的一个对象的完整表示。

编辑

声明你的数组“arr”来保存字典而不是字符串。就像错误状态一样:

var arr = [[String:String]]()

关于ios - 将循环中的对象从字典添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772300/

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