我在iOS应用中设置了以下功能: - (IBAction)nextButton:(id)sender{ if (self.itemSearch.text.length 0) { [PFCloud callFunctionInBackground:@"eBayCategorySearch" withParameters:@{@"item": self.itemSearch.text} block:^(NSString *res
- (IBAction)nextButton:(id)sender { if (self.itemSearch.text.length > 0) { [PFCloud callFunctionInBackground:@"eBayCategorySearch" withParameters:@{@"item": self.itemSearch.text} block:^(NSString *result, NSError *error) { NSLog(@"'%@'", result); NSData *returnedJSONData = result; NSError *jsonerror = nil; NSDictionary *categoryData = [NSJSONSerialization JSONObjectWithData:returnedJSONData options:0 error:&jsonerror]; NSArray *resultArray = [categoryData objectForKey:@"results"]; NSDictionary *dictionary1 = [resultArray objectAtIndex:1]; NSNumber *numberOfTopCategories = [dictionary1 objectForKey:@"Number of top categories"]; // NSDictionary *dictionary2 = [resultArray objectAtIndex:2]; // NSNumber *topCategories = [dictionary2 objectForKey:@"Top categories"]; NSDictionary *dictionary3 = [resultArray objectAtIndex:3]; NSNumber *numberOfMatches = [dictionary3 objectForKey:@"Number of matches"]; // NSDictionary *dictionary4 = [resultArray objectAtIndex:4]; // NSNumber *userCategoriesThatMatchSearch = [dictionary4 objectForKey:@"User categories that match search"]; if (!error) { // if 1 match found clear categoryResults and top2 array if ([numberOfMatches intValue] == 1 ){ [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self]; } // if 2 matches found else if ([numberOfMatches intValue] == 2){ [self performSegueWithIdentifier:@"ShowUserCategoryChooserSegue" sender:self]; //default to selected categories criteria -> send to matchcenter -> clear categoryResults and top2 array } // if no matches found, and 1 top category is returned else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 1) { [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self]; } // if no matches are found, and 2 top categories are returned else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 2) { [self performSegueWithIdentifier:@"ShowSearchCategoryChooserSegue" sender:self]; } } }]; } }
我要做的是决定采用哪个segue,具体取决于返回的JSON的键/值对.但是,当按下nextButton时,我的应用程序崩溃,并返回以下内容:
2014-05-02 14:51:18.623 Parse+Storyboard[1325:60b] '{ results = ( { "Number of top categories" = 2; }, { "Top categories" = ( 20349, 9355 ); }, { "Number of matches" = 0; }, { "User categories that match search" = ( ); } ); }' 2014-05-02 14:51:18.624 Parse+Storyboard[1325:60b] -[__NSDictionaryM bytes]: unrecognized selector sent to instance 0xaa9d090 2014-05-02 14:51:18.639 Parse+Storyboard[1325:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM bytes]: unrecognized selector sent to instance 0xaa9d090' *** First throw call stack: ( 0 CoreFoundation 0x02a771e4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x026358e5 objc_exception_throw + 44 2 CoreFoundation 0x02b14243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3 CoreFoundation 0x02a6750b ___forwarding___ + 1019 4 CoreFoundation 0x02a670ee _CF_forwarding_prep_0 + 14 5 Foundation 0x0237b4bc -[_NSJSONReader findEncodingFromData:withBOMSkipLength:] + 36 6 Foundation 0x0237b66b -[_NSJSONReader parseData:options:] + 63 7 Foundation 0x0237bc30 +[NSJSONSerialization JSONObjectWithData:options:error:] + 161 8 Parse+Storyboard 0x000039ab __35-[SearchViewController nextButton:]_block_invoke + 203 9 Parse+Storyboard 0x0007b217 __40-[PFTask thenCallBackOnMainThreadAsync:]_block_invoke_2 + 241 10 libdispatch.dylib 0x036877b8 _dispatch_call_block_and_release + 15 11 libdispatch.dylib 0x0369c4d0 _dispatch_client_callout + 14 12 libdispatch.dylib 0x0368a726 _dispatch_main_queue_callback_4CF + 340 13 CoreFoundation 0x02adc43e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 14 CoreFoundation 0x02a1d5cb __CFRunLoopRun + 1963 15 CoreFoundation 0x02a1c9d3 CFRunLoopRunSpecific + 467 16 CoreFoundation 0x02a1c7eb CFRunLoopRunInMode + 123 17 GraphicsServices 0x02cd45ee GSEventRunModal + 192 18 GraphicsServices 0x02cd442b GSEventRun + 104 19 UIKit 0x012f5f9b UIApplicationMain + 1225 20 Parse+Storyboard 0x00002fbd main + 141 21 libdyld.dylib 0x038d1701 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
我似乎无法弄清楚它指的是哪个选择器,以及为什么它无法识别.
从错误消息-[__NSDictionaryM bytes]: unrecognized selector sent to instance ...
和NSLog()输出
'{ ... }'
人们可以看到结果
不是字符串(包含JSON数据),而是NSDictionary.所以没有必要
使用NSJSONSerialization:
[PFCloud callFunctionInBackground:@"eBayCategorySearch" withParameters:@{@"item": self.itemSearch.text} block:^(NSDictionary *result, NSError *error) { NSArray *resultArray = [result objectForKey:@"results"]; // ... }];
另请注意,数组中的第一个数组的索引为零,因此您可能需要从resultArray而不是1 .. 4中检索索引为0 .. 3的对象.