2016-11-23 4 views
0

dictionaryからarrayにオブジェクトを追加したいと思います。辞書から配列へループのオブジェクトを追加

私はJSONから情報を取得しています。これらの情報を2つのキーと2つの値でループに追加したいと思います。"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

あなたの質問は** no ** senseとなります。あなたは辞書を持っていると言いますが、辞書があるようです。出力を配列にしたいとします。あなたのコードは文字列の配列を構築するように見えますが、出力はもっと長い文字列のように見えます。 –

+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) 

にあなたが辞書の完全な配列を持っているので、あなたは配列に辞書を追加しています。各辞書は、あなたのオブジェクトの1つを完全に表現したものです。

編集

代わりに文字列の辞書を保持するために、あなたの配列 "ARR" を宣言します。エラー状態と同様に:

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

OPの "arr"変数は文字列の配列として宣言されています。それは彼が投稿したコードと一致します。文字列オブジェクトの配列に辞書を追加できないため、コードが失敗します。 –

+0

タイプ '[String:String?]'の値を期待される引数タイプ 'String'に変換できません – Viny76

+0

おっと。良いキャッチ!それは非常に簡単な修正ですが、配列を宣言して辞書を保持するだけです。 – user3353890

関連する問題