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
Szabolcs Varga
Courses Plus Student 662 PointsParse.com issue: Why can't save this 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");
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)
1 Answer
Unsubscribed User
6,125 PointsThe problem is, that you add the username to self.mainArray, not the user object. Replace the right parts to these:
- (void)viewDidLoad {
[super viewDidLoad];
self.mainArray = [[NSMutableArray alloc] init];
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"];
PFUser *user = [self.mainArray objectAtIndex:indexPath.row];
cell.usernameLabel.text = user.username;
[cell.addButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (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];
} else {
[self.mainArray addObject:user];
}
[tableView reloadData];
}
}];
}