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"]]
どうすればこの問題を解決できますか?
あなたの質問は** no ** senseとなります。あなたは辞書を持っていると言いますが、辞書があるようです。出力を配列にしたいとします。あなたのコードは文字列の配列を構築するように見えますが、出力はもっと長い文字列のように見えます。 –
@DuncanC私が誤解していない限り、彼は出力が辞書でいっぱいの配列であることを望んでいると思う。ちょうど "私は持っていると思います"というテキストに基づいています。私は間違っているかもしれません!私はしばしばです。 – user3353890
あなたは単に 'arr.append(myDictionary)'をしないのですか? – njzk2