简体   繁体   中英

'[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
}

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

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM