Motivation gym5
Motivation gym5

Reputation: 291

'[AnyObject]?' is not convertible to 'NSArray'(xcode,swift)

The error is in let USER:PFUser... It was working good but when I updated Xcode this problem appeared. The name of the username is not being displayed.

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let postcells: postsTableViewCell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! postsTableViewCell

    let Post:PFObject = self.PostData.objectAtIndex(indexPath.row) as! PFObject


    postcells.postTimetextView.text = Post.objectForKey("Content") as! String


    var dateFormatter:NSDateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    postcells.timeStamp.text = dateFormatter.stringFromDate(Post.createdAt!)


    var findPostedBy:PFQuery = PFUser.query()!

    findPostedBy.whereKey("objectId", equalTo: Post.objectForKey("Postedby")!)

    findPostedBy.findObjectsInBackgroundWithBlock{
        (objects, error) -> Void in
            if error == nil {
                let USER:PFUser = (objects as NSArray).lastObject as! PFUser
                postcells.usernameLabel3.text = USER.username



            }
        }

    return postcells
}

Upvotes: 0

Views: 276

Answers (1)

Fred Faust
Fred Faust

Reputation: 6800

You need to make sure that an array was returned from parse

You can use this:

findPostedBy.findObjectsInBackgroundWithBlock{
    (objects, error) -> Void in
        if error == nil {

            if objects?.count > 0 {

                let USER:PFUser = objects!.last as! PFUser

            // Avoid updating the UI on a background thread

               dispatch_async(dispatch_get_main_queue(), { () -> Void in
                postcells.usernameLabel3.text = USER.username
            })
            }

        }
    }

Upvotes: 1

Related Questions