The array contained in the key "children" contains 100 items.
Is there away to tell SwiftyJSON to grab a random index? I tried creating a random number
var random = arc4random_uniform(24)
but when I inserted random I got the error:
"Cannot subscript a value of type 'JSON' with an index of type 'UInt32'"
I also converted it to an NSNumber and same thing, I'm completely lost.
func getBackgoundImageData(completed: @escaping DownloadComplete) {
let imageURL = URL(string: IMAGE_URL)!
Alamofire.request(imageURL).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
if let url = json["data"]["children"][0]["data"]["preview"]["images"][0]["source"]["url"].string {
self._backgroundImageURL = url
}
case .failure(let error):
print(error)
}
completed()
}
}
["data"]["children"][0]["data"]["preview"]["images"][0]["source"]["url"]you are going to have problems as Swift may not be able to infer the types of all those access correctly. Split this line out into a series of conditional downcastsif let data = json["data"] as [String:Any] ...and so on until you get your final array and then use the random index on that