chathura
chathura

Reputation: 161

Cannot query the username from the Parse iOS Swift

im new to iOS and developing twitter like app using tutorials.

people can make tweets from their account. and it get display in home page.

at the moment to home page all the tweets and everything works well except username. im getting nil

this is what i have tried

    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell:SpreadTableViewCell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpreadTableViewCell


            let spread:PFObject = self.LiveFeedData.objectAtIndex(indexPath.row) as! PFObject
            //----------This Content Works fine ----------------
            cell.spreadTextView.text = spread.objectForKey("content") as! String 

            //Spreader
            var findSpreader:PFQuery = PFUser.query()!
            findSpreader.whereKey("objectId", equalTo: spread.objectForKey("spreader")!)


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

                if error==nil{
                    let user = (objects as! [PFUser]).last
//but this one giving me nil
                    cell.username.text = user?.username

                }

            }

 return cell
    }

enter image description here

enter image description here

Upvotes: 0

Views: 202

Answers (1)

Swinny89
Swinny89

Reputation: 7373

Try this:

var user = spread.objectForKey("spreader") as! PFUser
user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
        if obj != nil {
            var fetchedUser = obj as! PFUser
            var username = fetchedUser["username"] as! String
            println(username) 
        }
    }

Upvotes: 1

Related Questions