当当前用户点击按钮时,我想将联系人添加到当前用户的联系人列表中addToContact
,但是当我点击它时出现错误。我不知道为什么会发生这种情况,因为代码基于 Parse 指南,因此如果有人能告诉我正确的方向,那将是一个巨大的帮助。我还尝试了另一种变体,它不起作用,您可以在 .m 文件下查看此代码。
这是错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString parseClassName]: unrecognized selector sent to instance 0x16552d80'
我的 .m 文件:
@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");
}
我的第二次尝试:
- (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"); }
这是错误:
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