How to set the current user in cell? Swift, Parse

2019-09-19 10:54发布

问题:

Day, guys. Help me.I can not solve the problem for three days

I make an app look like an Instagram. I want to make sure that when you create a post, in cell of tableview, writing his nickname.

I am try this:

findSweeter.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]?, error:NSError?)->Void in
        if error == nil{
            let user:PFUser = (objects as NSArray).lastObject  as PFUser!
            cell1.usernamelabel.text = user.username
        }
    }
}

But thats issue: [AnyObject]? is not convertible to 'NSArray' & 'NSArray is not convertible to PFUser'

Help, pls. im using Parse.com

回答1:

I solved my problem so easly. I just changed PFUser variable in Data on Parse Clotd to String:

cell1.usernamelabel.text = sweet.objectForKey("usa") as? String



回答2:

The problem isn't the cast, but the fact that objects is an optional array: [AnyObject]?

You have to unwrap the array first:

findSweeter.findObjectsInBackgroundWithBlock{
    (objects:[AnyObject]?, error:NSError?)->Void in
    if error == nil && indexPath.row < objects?.count{
       var user: PFUser
       if let userObject = objects?[indexPath.row] {
          user = userObject as! PFUser
          cell1.usernamelabel.text = user.username
       }
    }
}