从Dictionary到Array循环添加对象

时间:2016-11-23 19:00:50

标签: ios arrays swift dictionary

我想将dictionary的对象添加到array

我从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"]]

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

更改代码的这一部分

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]]()