ios - 获取要保存的 likeCount 时出错

标签 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 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/

相关文章:

ios - WKInterfacelabel 检查是否被截断

ios - 使用 NSPredicate 实现高效的 PFQuery

ios - NSURLSessionDataTask 不从请求返回数据

android - 原生移动应用程序之上的 HTML5 层

ios - -[__ NSArrayI objectForKeyedSubscript:]:无法识别的选择器已发送到实例IN Xcode6 Objective-C iOS

objective-c - 在 NSMenuItem 上设置标题,没有效果

ios - 如何使用 ios 图表在条形图中点击条形图时在条形图上显示值?

javascript - 在 Parse 中检索字符串

ios - 无法使用 geoPointForCurrentLocationInBackground 检索地理点

ios - 使用不同帧 iOS 重复的 UIView 连续动画