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 Build a Self-Destructing Message iPhone App Relating Users in Parse.com Removing Friends by Tapping on a Table View Cell

How does isFriend know what self is?

In this method, how is self equal to PFUser?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

method:

-(BOOL)isFriend:(PFUser *)user {
    for (PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            return YES;
        }
    }
    return NO;
}

2 Answers

Stone Preston
Stone Preston
42,016 Points

self is not a PFUser. self is the viewController object, however the method named isFriend is an instance method of the viewController class. isFriend is just a method that takes a PFUser as its argument and that method is an instance method of your viewController, which is why you call it using self.

the method call is a bit confusing if you try and read it like english. it reads something like this literally: "if self isFriend:user then do this" but really what it should be translated to is "if the isFriend method that belongs to my viewController returns true when passed this user as an argument then do this"

So self is only there to be able to call the method isFriend? it's not being called on self itself right?

doublepost

Stone Preston
Stone Preston
42,016 Points

well, it is being called on self. the isFriend method is an instance method of your view controller, so its called on an object of that class.

lets think about it in terms of messages instead of methods to try and clear it up. you have an isFriend message. you send the isFriend message to your viewController object (through the use of the self keyword) and pass in the user as an argument to that message. the viewController object responds to that message. if you get true, that user is a friend, if you get false, that user is not a friend

Got it! Thanks again Stone! Really helpful.