Gijo Varghese Gijo Varghese - 23 days ago 4
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: "")]

Alamofire.request("", 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

But it is giving me the following error:

enter image description here


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 {