Kody R. Kody R. - 27 days ago 11
Swift Question

Error: 'Type of expression is ambiguous without more context'

I'm pretty new to coding Swift, so please excuse me if this error is a simple answer!

I keep getting an error message that says "Type of expression is ambiguous without more context."

var findTimelineData: PFQuery = PFQuery(className: "Sweets")
findTimelineData.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error:NSError?) -> Void in

if error == nil {
for object:PFObject in objects! { // ----This is the error line---
self.timelineData.addObject(object)
}
}
}


I understand what the problem is, I'm just not certain how to go about fixing it. I've seen other questions about this answered, but none about iterating through an array of AnyObject.

Thanks!

Answer

You can help the compiler know what objects is like this:

for object in objects as! [PFObject] {
    self.timelineData.addObject(object)
}
Comments