Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
            }
        }
    }

Any suggestions?

Thanks!

like image 269
Kody R. Avatar asked Jul 03 '15 17:07

Kody R.


3 Answers

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

for object in objects as! [PFObject] {
    self.timelineData.addObject(object)
}
like image 65
Eric Aya Avatar answered Oct 10 '22 17:10

Eric Aya


if let pfObjects = objects as? [PFObject]
{
    for pfObject in pfObjects
    {
        self.timelineData.addObject(pfObject)
    }
}

...exclamation points in Swift code give me the heeby jeebies.

like image 26
Tom Howard Avatar answered Oct 10 '22 18:10

Tom Howard


If you are writing some code likes:

for (i, view) in views { 
}

You need to add enumerated:

for (i, view) in views.enumerated() {
}

And now it should work.

like image 3
Zigii Wong Avatar answered Oct 10 '22 18:10

Zigii Wong