objective-cparse-platformpfrelation

Parse.com issue: Why can't save the PFRelation?


I would like to add a contact to the current users contact list when the current user taps the addToContact button, but i get an error when i tap it. I have no idea why this happens, because the code based on the Parse guide, therefore it would be a huge help if somebody could show me the right direction. I also tried one other variation, that doesn't work, you can see this code under the .m file.

Here's the error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString parseClassName]: unrecognized selector sent to instance 0x16552d80'

My .m file:

@implementation TestViewController

    - (void)viewDidLoad
    {
    [super viewDidLoad];

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


    }

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

    return [self.mainArray count];
}

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


    DevTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];

    cell.usernameLabel.text = [self.mainArray objectAtIndex:indexPath.row];
    [cell.addButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];


    return cell;
}

- (IBAction)addTheContact:(id)sender {


    PFUser *theNewContact = [self.mainArray firstObject];
    NSLog(@"username: %@",theNewContact);
    PFRelation *contactList = [self.currentUser relationforKey:@"contactList"];
    [contactList addObject:theNewContact];
    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error){
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];
}

- (IBAction)searchButton:(id)sender {

    NSString *searchResult = [self.searchField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    PFQuery *query = [PFUser query];
    [query whereKey:@"username" equalTo:searchResult];
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (object) {
            PFUser *user = (PFUser *)object;
            NSLog(@"Username: %@", user.username);
            if (self.mainArray.count > 0) {
                [self.mainArray replaceObjectAtIndex:0 withObject:user.username];
            } else {
                [self.mainArray addObject:user.username];
            }

            [tableView reloadData];

        }
    }];
}

- (IBAction)logout:(id)sender {
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
- (void)didTapButton:(id)sender {

    // Cast Sender to UIButton
    UIButton *button = (UIButton *)sender;

    // Find Point in Superview
    CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:tableView];

    // Infer Index Path
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pointInSuperview];

    NSLog(@"Hello World");
}
  1. My second try:

    - (IBAction)addTheContact:(id)sender { self.contactRecipient = [self.mainArray firstObject]; PFQuery *queryContact = [PFUser query]; [queryContact whereKey:@"username" equalTo:self.contactRecipient]; PFObject *recentContact = [queryContact getFirstObject]; PFUser *theNewContact = [recentContact objectForKey:@"user"]; PFRelation *contactList = [self.currentUser relationforKey:@"contactList"]; [contactList addObject:theNewContact]; [self.currentUser saveInBackground]; NSLog(@"Bug test log"); }

and this is it's error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x3052fe8b 0x3a82a6c7 0x304690cb 0x30472a51 0x1142c7 0xdecfb 0x32cea55f 0x32cea4fb 0x32cea4cb 0x32cd60f3 0x32ce9f13 0x32ce9bdd 0x32ce4c09 0x32cb9f59 0x32cb8747 0x304faf27 0x304fa3ef 0x304f8bdf 0x30463541 0x30463323 0x3519a2eb 0x32d1a1e5 0xddbc9 0x3ad23ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException 

Solution

  • You initialize array with a string

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

    and then trying to work with the firstObject (the string) as if it's a PFUser

    PFUser *theNewContact = [self.mainArray firstObject];
    

    and

    self.contactRecipient = [self.mainArray firstObject];