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!

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

Adding Users (Friends) In Ribbit PLEASE HELP

hey i want to be able to add certain users in the app ribbit as now in the tutorial you cant friends by the username please help!

7 Answers

Ok thanks ben um im not that smart but on the tutorial i follwed it but in which part do you put the "whereKey" piece of code?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

We talk about adding a "whereKey" in this video: http://teamtreehouse.com/library/build-a-selfdestructing-message-iphone-app-2/retrieving-and-viewing-data-from-parsecom/retrieving-data-from-parsecom

Check that out and see if it explains it well enough. If not, pop back in here with questions and we can talk through it some more. :)

I haven't understood your request. Could you please explain in more detail?

When a user signs up they have no friends. I want their to be a button like +Firends or something like that. when they click it they can search for a friend my username how do i do this?

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Have you gotten to the part of the tutorial where we add the "Edit Friends" screen? That provides a very simple list to choose from, but making a search based off that isn't too much harder. Instead of querying Parse for the entire list of users, you would need to add a whereKey to look for specific username matches. We talk about adding whereKeys later in the tutorial as well.

I would recommend completing the whole app and then going back to add that kind of functionality (which is a great idea, by the way).

Hi! Ben Jakuben

I have the same problem. See my errors and code bellow.

1) Error: - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

Issue: Use of undeclared identifier 'searchBarCancelButton Clicked

2) Error: PFQuery *query = [PFUser query]; [query orderByAscending:@"username"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"Error: %@ %@", error, [error userInfo]); } else { self.allUsers = objects; [self.tableView reloadData]; } }];

Issue: - Use of undeclared identifier 'query' - Use of undeclared identifier 'query'

This issue is marked over: [query orderByAscending:@"username"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

Here is my code

            <p>
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface EditFavoritesViewController : UITableViewController <UISearchBarDelegate>


@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

- (BOOL)isFriend:(PFUser *)user;

@end




#import "EditFavoritesViewController.h"
#import <Parse/Parse.h>


@interface EditFavoritesViewController ()

@end

@implementation EditFavoritesViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar.delegate = self;

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

        //dismiss keyboard
        [self.searchBar resignFirstResponder];

    }

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

        //Enable the cancel button when the user touches the search field
        self.searchBar.showsCancelButton = TRUE;
    }

    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {

        //dsiable the cancel button when the user ends editing
        self.searchBar.showsCancelButton = FALSE;
    }

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

        //dismiss keyboard
        [self.searchBar resignFirstResponder];


        //Strip the whitespace off the end of the search text
        NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


        //Check to make sure the field isnt empty and Query parse for username in the text field
        if (![searchText isEqualToString:@""]) {

            PFQuery *query = [PFUser query];
            [query whereKey:@"username" equalTo:searchText];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {

                    //check to make sure the query actually found a user
                    if (objects.count > 0) {

                        //found a user
                        //set the user as the table views data source and reload the table view


                        //A user was not found, display error message
                    } else {

                        //no user found

                    }

                    [self.tableView reloadData];

                } else {

                    //error occurred
                }
            }];

        }


    }


    PFQuery *query = [PFUser query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {
            self.allUsers = objects;
            [self.tableView reloadData];
        }
    }];

    self.currentUser = [PFUser currentUser];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.allUsers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
    cell.textLabel.text = user.username;

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

    if ([self isFriend:user]) {
        cell.accessoryType = UITableViewCellAccessoryNone;

        for(PFUser *friend in self.friends) {
            if ([friend.objectId isEqualToString:user.objectId]) {
                [self.friends removeObject:friend];
                break;
            }
        }

        [friendsRelation removeObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.friends addObject:user];
        [friendsRelation addObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

#pragma mark - Helper methods

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

    return NO;
}

@end</p>
            ```
Ben Jakuben
Ben Jakuben
Treehouse Teacher

In this code you have two PFQuery variables names "query", which is giving you the error. Change the second to "query2" or something else and hopefully you can continue from there.

Anyone?

Did you get it to work. Please post code. Karma is watching the webb.