2016-11-23 99 views
0

我想將對象從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"]] 

我該如何解決這個問題?

+0

你的問題使**沒有**感。你說你有一本字典,但看起來你有一本字典。你說你想要輸出是一個數組。您的代碼看起來像構建了一個字符串數組,但是您的輸出看起來更像是一個單獨的長字符串。 –

+0

@DuncanC除非我誤解,我相信他希望輸出是一個充滿字典的數組。只是基於「我想擁有」文本。但我可能會誤認爲!我經常是。 – user3353890

+1

爲什麼你不只是'arr.append(myDictionary)'? – njzk2

回答

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

OP的「arr」變量被聲明爲一個字符串數組。這與他發佈的代碼相符。您的代碼會失敗,因爲您無法將字典附加到String對象數組。 –

+0

無法將類型'[String:String?]'的值轉換爲預期的參數類型'String' – Viny76

+0

哎呀。接得好!這是一個非常簡單的解決方法,只需聲明數組來保存字典。 – user3353890