現在のユーザーがボタンをタップしたときに現在のユーザーの連絡先リストに連絡先を追加したいのですが、タップ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");
}
私の2回目の試み:
- (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