Gijo Varghese Gijo Varghese - 2 months ago 15
Swift Question

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 use 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/gijovarghese141@gmail.com/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

Answer

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