0

I would like to add object from dictionary to array.

I'm getting info from JSON, and I would like to add in loop these info with two keys and two values : "name": value1, "imageURL": value2.

I have 10 items in my loop, so I tried this code, but [] is missing :

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

that gives me this :

["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"]

Now, I would like to have this (add [] at each object in loop) :

[["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"]]

How can I fix this ?

3
  • Your question makes no sense. You say you have a dictionary, but it appears you have a dictionary. You say you want the output to be an array. Your code looks like it builds an array of strings, but your output looks more like a single long string. Commented Nov 23, 2016 at 19:10
  • @DuncanC unless i'm misunderstanding, I believe he wants the output to be an array full of dictionaries. Just based on the "I would like to have" text. I could be mistaken though! I often am. Commented Nov 23, 2016 at 19:18
  • 1
    why don't you just arr.append(myDictionary)? Commented Nov 23, 2016 at 19:28

1 Answer 1

1

Change this part of your code

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

To this

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

You're adding the dictionary to an array so you have an array full of dictionaries. Each dictionary is a full representation of one of your objects.

Edit

declare your array "arr" to hold dictionaries instead of Strings. Just like the error states:

var arr = [[String:String]]()
Sign up to request clarification or add additional context in comments.

10 Comments

The OP's "arr" variable is declared as an array of strings. That matches the code he posted. Your code would fail since you can't append a dictionary to an array of String objects.
Cannot convert value of type '[String : String?]' to expected argument type 'String'
Whoops. good catch! That's a pretty easy fix though, just declare the array to hold dictionaries.
just made an edit. it should hopefully work now, but let me know if there are any other issues.
@user3353890 thanks man for your help, but again an error : Value of type '[String : String]' has no member 'append'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.