Gijo Varghese
Gijo Varghese

Reputation: 11780

Add elements from SwiftyJSON to URL array in Swift

I'm retrieving a list of URL images using Alamofire. The response is in JSON And I have used SwiftyJSON to parse and print each element. I want the URLs to be added into a URL array. Below is the code I have used

var newArray = [URL(string: "http://www.tummyvision.com/users/uploads/[email protected]/photos/4.jpg")]

        Alamofire.request("http://www.tummyvision.com/users/login/get-images.php", parameters: parameters).responseData { response in
            let json = JSON(data: response.result.value!)
            for i in 0..<json.count
            {
                print(json[i]) // prints the correct url
                self.urlArray.append(json[i])
            }
        }

But it is giving me the following error:

enter image description here

Upvotes: 0

Views: 615

Answers (1)

Nirav D
Nirav D

Reputation: 72410

Try to use arrayValue property of JSON to access array also you need to convert String Url to URL object before adding it into Array of URL.

if let urls = json.arrayValue {
    for url in urls {
        self.urlArray.append(URL(string:url))
    }
}

Upvotes: 2

Related Questions