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


Szabolcs Varga
Szabolcs Varga
Courses Plus Student 662 Points

How to pass a value from NSMutableArray to a PFUser object?

I have a mutable array, called mainArray. It contains a username that is the search result of a user search. I'm displaying every search result in a custom table view cell, that also have a UIButton called addButton. I would like to add the user to the friends list of the current user, when the current user taps the addButton.

The problem is that i can not assign the value from "mainArray" to the "newUser". I've tried some solutions, but the result was always an error or freeze, therefore it would be amazing, if somebody could help me to solve this problem.

My last try wast this line: (it makes this warning: Incompatible pointer types sending NSIndexPath * to parameter of type NSString)

[newUser addUniqueObjectsFromArray:mainArray forKey:indexPath];

Dávid Szép van valami otleted?


  • (void)viewDidLoad { [super viewDidLoad];

    mainArray = [[NSMutableArray alloc] initWithObjects:@"User", nil];
    self.currentUser = [PFUser currentUser];


    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [mainArray count];


    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    DevTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
    cell.usernameLabel.text = [mainArray objectAtIndex:indexPath.row];
    [cell.addButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;


    • (void)didTapButton:(id)sender {

      UIButton *button = (UIButton *)sender; CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:tableView]; NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pointInSuperview];

      PFUser *newUser = [PFUser user]; // The problem is here. [newUser addUniqueObjectsFromArray:mainArray forKey:indexPath];

      PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];

      if ([self isFriend:newUser]) {

      for (PFUser *friend in self.friends) {
          if ([friend.objectId isEqualToString:newUser.objectId]){
              NSLog(@"some log");

      } else {

      [self.friends addObject:newUser];
      [friendsRelation addObject:newUser];


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

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

2 Answers

I would suggest that you review the Parse documentation for PFUser. There is inherint security applied to PFUser objects which prevents the modification of user except by that actual user once authenticated. Since you are creating a new PFUser i do not think you will be allowed to modify it, but I may be wrong. Have you posted this question on the Parse forums?


Szabolcs Varga
Szabolcs Varga
Courses Plus Student 662 Points

Thank you Dennis, i've read carefully the Parse docs and i have to rewrite this part of my code because the PFQuery and Relation offers some other easier solutions than this.

You con't modify the PFUser on the client. You can use the master key in Cloud Code and write all the modifications in Cloud Code. (Use the useMasterKey() function.) https://parse.com/questions/pfuser-can-we-modify-data-of-a-user-from-an-other-user-logged-in

You can begin with this: https://parse.com/docs/cloud_code_guide

Szabolcs Varga
Szabolcs Varga
Courses Plus Student 662 Points

I need to use Cloud Code? Or it's just an alternative?

Sorry for the delay. The Cloud Code is just an alternative. If you will use this, you just need to add a new column to User class with Relation type and User target class. Thereafter in Cloud Code you add the "friend user" to the logged user's relations, and logged user to the "friend user's" relations.

The other solution, that you add a new class to your database with two Pointer with User target class. You can add new records to this class from client, because the users don't change.

Write me if you need more help. :)