Ios 获取要保存的likeCount时出错
我正在尝试用likeCount更新ios中的对象。下面是我当前的代码和错误Ios 获取要保存的likeCount时出错,ios,objective-c,parse-platform,counter,Ios,Objective C,Parse Platform,Counter,我正在尝试用likeCount更新ios中的对象。下面是我当前的代码和错误 NSNumber *likeCount = [self.currentItem.pfObj valueForKey:@"likeCount"]; NSLog(@"Initial number of likes ---> %@", likeCount); likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1]; NSLog(@"New
NSNumber *likeCount = [self.currentItem.pfObj valueForKey:@"likeCount"];
NSLog(@"Initial number of likes ---> %@", likeCount);
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
NSLog(@"New number of likes ---> %@", likeCount);
PFQuery *query = [PFQuery queryWithClassName:@"MainItem"];
[query getObjectInBackgroundWithId:self.currentItem.likeCount
block:^(PFObject *upLikeCount, NSError *error) {
NSLog(@"Post query number of likes ---> %@", likeCount);
upLikeCount = likeCount;
NSLog(@"New count of likes ---> %@", upLikeCount);
[upLikeCount saveInBackground];
}];
like计数在日志中按预期增加,但我无法将其保存到对象。我最初是用count创建一个新对象。现在(上面的代码)我正在查询项,但在xCode和
在日志中:
Initial number of likes ---> 1
New number of likes ---> 2
-[__NSCFNumber length]: unrecognized selector sent to instance 0x17d12345
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x17d12345'
感谢您的帮助您需要将值指定给对象中的字段,而不是对象本身 此外,由于您已经拥有该对象,因此无需先获取它-您只需保存它即可-
NSNumber *likeCount = [self.currentItem.pfObj valueForKey:@"likeCount"];
NSLog(@"Initial number of likes ---> %@", likeCount);
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
NSLog(@"New number of likes ---> %@", likeCount);
self.currentItem.pfObj[@"likeCount"]=likeCount;
[self.currentItem.pfObj saveInBackground];
您可以使用PFObject
方法incrementKey
进一步简化代码
[self.currentItem.pfObj incrementKey:@"likeCount"];
[self.currentItem.pfObj saveInBackground];
谢谢你,保罗!这正是我需要的。我被别人甩了