I was following a tutorial to load Image URL's and appending them to an array to display user profile images in a UITableView of cells for each user, using the following block of code:
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), {
//"profilePics" is my array of UIImages made as a global var
self.profileImages.append(UIImage[data!])
})
}).resume()
But for some reason "NSURLSession" does not appear as a suggestion while I am programming and it returns errors, so I changed the everything from "NSURL..." to simply "URL..." :
//Grabbing images from db
print("starting get image block : ")
let url = URL(string: profileImageURL)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
print("grabbing image: ") //Test line for monitor
print(String(describing: data)) //Test line for monitor : returns "nil"
if error != nil{
print(error!)
return
}
DispatchQueue.main.async {
//"profilePics" is my array of UIImages made as a global var
self.profilePics.append(UIImage(data: data!))
print("Image appended")
}
}).resume()
// grabbing images from db end
But each time I run this I get a nil for data, and it is never appended to my array. Can someone explain what I am missing? This is my first attempt with Firebase.