I cannot get a [UIImage?] return from this function. The getDataInBackgroundWithBlock
won\'t let me set a return value other than Void in
. However, tha
As your function states the block is executed in the background (on a asynchronous thread. This means that it will load in the background but will also continue the rest of the function thus returning an empty array.
To fix this you should use a completion handler for your block in background.
func queryImages(onComplete:(images: [UIImage?])-> Void){
var iconArray: [UIImage?] = []
var query: PFQuery = PFQuery(className: "QuestionMaster")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
for object in objects! {
let imageFiles = object["questionImage"] as! PFFile
let imageData = imageFiles.getData()
let image = UIImage(data:imageData!)
iconArray.append(image)
println(iconArray)
}
onComplete(images: iconArray)
}
}
Although untested above code should work
The Asynchronous data retrieval inside your for loop has been replaced in favor of a synchronous call to ease the project