Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[AnyObject]?' does not have a member named 'Generator'

func retrieveMessages() {

    var query:PFQuery = PFQuery(className: "Message")

    //Call findobjectsinbackground

    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in

        self.messagesArray = [String]()
        for messageObject in objects {   <<<<<<<<<<<<<<<<< error
            let messageText:String? = (messageObject as PFObject)["Text"] as? String
            if messageText != nil {
                self.messagesArray.append(messageText!)
            }
        }
        self.messageTableView.reloadData()
    }
}

From this code an error occurs saying: [AnyObject]? does not have a member named 'Generator'. Is there a way how to correct this?

like image 344
Pondpoud Avatar asked Dec 08 '22 03:12

Pondpoud


1 Answers

Your objects array is declared as an Optional : objects:[AnyObject]?

So you need to unwrap it before looping over it:

for messageObject in objects! {
    // do stuff
}

And since objects can be nil, better do this:

if let myObjects = objects {
    for messageObject in myObjects {
        // do stuff
    }
}
like image 59
Eric Aya Avatar answered Dec 11 '22 09:12

Eric Aya