Error: 'Type of expression is ambiguous withou

2019-01-25 07:06发布

问题:

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!

回答1:

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

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


回答2:

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

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



回答3:

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.