Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取要保存的likeCount时出错_Ios_Objective C_Parse Platform_Counter - Fatal编程技术网

Ios 获取要保存的likeCount时出错

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

我正在尝试用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];
       }];
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];

谢谢你,保罗!这正是我需要的。我被别人甩了