0

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

3
  • What data structure is spread? Commented Aug 9, 2015 at 13:03
  • @Swinny89 Updated the code Commented Aug 9, 2015 at 13:07
  • You should do something if error isn't nil - print it, for example. You can simplify your code however, the spreader value in your record is a PFUser, so instead of running a find, you can just read it using fetchInBackgroundWithBlock Commented Aug 9, 2015 at 13:09

1 Answer 1

1

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) 
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Not a problem, glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.