Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Chris Bracewell
Chris Bracewell
34 Points

Adding a table row object to a PFRelation

I'm creating a table with a list of users, when the users are pressed they are then added to a PFRelation in the user's class. When I write objectAtIndex it underlines it and throws an error. The Code is..

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark

        var relation : PFRelation = PFUser.currentUser().relationForKey("friendship")
        let user:PFObject = self.userArray.objectAtIndex(indexPath.row) as PFObject

        relation.addObject(user)
        PFUser.currentUser().saveInBackgroundWithBlock { (succeed:Bool, error: NSError!) -> Void in
            if error != nil {
                NSLog("Unable to save")
            }
        }

    }

The array if defined here..

    var userArray: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()


        var query = PFUser.query()
        query.whereKey("username", notEqualTo: PFUser.currentUser().username)
        var users = query.findObjects()
        for user in users {

            userArray.append(user.username)

        }
    } 

Any help would be greatly appreciated. Thanks, Chris

3 Answers

What error is thrown?

JIHOON JUNG
JIHOON JUNG
10,927 Points

var allUsers:NSArray = [] // NSArray *allUsers . . . try this

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.tableView.deselectRowAtIndexPath(indexPath, animated: false)

    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell!.accessoryType = UITableViewCellAccessoryType.Checkmark

    var user = self.allUsers[indexPath.row] as! PFUser
    var friendsRelation = self.currentUser!.relationForKey("friendsRelation")
    friendsRelation.addObject(user)
    self.currentUser!.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            // The post has been added to the user's likes relation.
        } else {
            // There was a problem, check error.description
            println("Error: \(error!)\(error!.userInfo)")

        }
    }
}
JIHOON JUNG
JIHOON JUNG
10,927 Points

and dont forget to add this

class EditFriendsController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

}

stuck in at least a couple of hours because of this "UITableViewDataSource & UITableViewDelegate"