我正在尝试使用 likeCount 更新 ios 中的对象。以下是我当前的代码和错误
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];
}];
点赞数按日志中的预期增加,但我无法将其保存到对象中。我最初是用计数创建一个新对象。现在(上面的代码我正在查询项目但在 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];
关于ios - 获取要保存的 likeCount 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215525/