Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary query returning "unrecognized selector sent to instance" error

I'm setting up the following function in my iOS app:

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

                                        }
                                    }];
    }
}

What I'm trying to do is decide which segue to take, depending on the key/value pairs of the JSON being returned. However, when the nextButton is pressed, my app crashes, and the following is returned:

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

I can't seem to figure out which selector it's referring to, and why it's unrecognized.

like image 336
Ghobs Avatar asked Jun 24 '26 11:06

Ghobs


2 Answers

From the error message

 -[__NSDictionaryM bytes]: unrecognized selector sent to instance ...

and the NSLog() output

'{ 
   ...
}'

one can see that the result is not a string (containing JSON data), but a NSDictionary. So there is no need to use NSJSONSerialization:

[PFCloud callFunctionInBackground:@"eBayCategorySearch"
                   withParameters:@{@"item": self.itemSearch.text}
                            block:^(NSDictionary *result, NSError *error) {

        NSArray *resultArray = [result objectForKey:@"results"];
        // ...
}];

Note also that the first array in an array has index zero, so you probably want to retrieve the objects with index 0 .. 3 from resultArray instead of 1 .. 4.

like image 59
Martin R Avatar answered Jun 26 '26 01:06

Martin R


You can't assign NSString directly to NSData like:

NSData *returnedJSONData = result;

Change that to:

NSData *returnedJSONData = [result dataUsingEncoding:NSUTF8StringEncoding];

According to callFunctionInBackground:withParameters:block: documentation the type of result is id.

So from your crash log and this error message -[__NSDictionaryM bytes]: I suspect that you are getting NSDictionary as response.

So change your method like:

[PFCloud callFunctionInBackground:@"eBayCategorySearch" withParameters:@{@"item": self.itemSearch.text} block:^(NSDictionary *categoryData, NSError *error) {
        NSLog(@"'%@'", categoryData);
        NSArray *resultArray = [categoryData objectForKey:@"results"];
        NSDictionary *dictionary1 = [resultArray objectAtIndex:1];
        NSNumber *numberOfTopCategories = [dictionary1 objectForKey:@"Number of top categories"];


        NSDictionary *dictionary3 = [resultArray objectAtIndex:3];
        NSNumber *numberOfMatches = [dictionary3 objectForKey:@"Number of matches"];
        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];
              }

        }
}];
like image 36
Midhun MP Avatar answered Jun 26 '26 00:06

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!