-6ren">
gpt4 book ai didi

ios - 返回 "unrecognized selector sent to instance"错误的 NSDictionary 查询

转载 作者:可可西里 更新时间:2023-11-01 17:07:22 24 4
gpt4 key购买 nike

我正在我的 iOS 应用程序中设置以下功能:

    - (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];
}

}
}];
}
}

我想做的是根据返回的 JSON 的键/值对决定采用哪个 segue。但是,当按下 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 的对象。

关于ios - 返回 "unrecognized selector sent to instance"错误的 NSDictionary 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com