views:

65

answers:

1

Hi all, i try to convert NSDictionary element to a NSData element:

...    
while ((key = [enumerator nextObject])) 
    {
        NSString *temp = [dictionary objectForKey:key];
        if ([key isEqualToString:@"results"]) 
        {
            //Crash HERE! -------------------------->>>>
            NSData *myData2 = [temp dataUsingEncoding:NSUTF32BigEndianStringEncoding];

            NSDictionary *dictionary2 = [[CJSONDeserializer deserializer] deserializeAsDictionary:myData2 error:&error];
            NSLog(@"Error: %@",error);
            //print dictionary
            NSEnumerator *enumerator2 = [dictionary2 keyEnumerator];
            id key2;
            while (key2 = [enumerator2 nextObject]) 
            {
                NSLog(@"Chiave: %@ | valore: %@",key2,[dictionary objectForKey:key2] );
            }

        }
    }
...

but in this way crash with:

    -[__NSArrayI dataUsingEncoding:]: unrecognized selector sent to instance 0x5b1c590
    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI dataUsingEncoding:]: unrecognized selector sent to instance 0x5b1c590'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x025fe919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0274c5de objc_exception_throw + 47
    2   CoreFoundation                      0x0260042b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x02570116 ___forwarding___ + 966
    4   CoreFoundation                      0x0256fcd2 _CF_forwarding_prep_0 + 50
    5   taxi rider                          0x00002722 -[taxi_riderViewController viewDidLoad] + 1508
    6   UIKit                               0x00375c26 -[UIViewController view] + 179
    7   taxi rider                          0x00001f61 -[taxi_riderAppDelegate application:didFinishLaunchingWithOptions:] + 74
    8   UIKit                               0x002ce543 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    9   UIKit                               0x002d09a1 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346
    10  UIKit                               0x002da452 -[UIApplication handleEvent:withNewEvent:] + 1958
    11  UIKit                               0x002d3074 -[UIApplication sendEvent:] + 71
    12  UIKit                               0x002d7ac4 _UIApplicationHandleEvent + 7495
    13  GraphicsServices                    0x02c78afa PurpleEventCallback + 1578
    14  CoreFoundation                      0x025dfdc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    15  CoreFoundation                      0x02540737 __CFRunLoopDoSource1 + 215
    16  CoreFoundation                      0x0253d9c3 __CFRunLoopRun + 979
    17  CoreFoundation                      0x0253d280 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x0253d1a1 CFRunLoopRunInMode + 97
    19  UIKit                               0x002d0226 -[UIApplication _run] + 625
    20  UIKit                               0x002dbb58 UIApplicationMain + 1160
    21  taxi rider                          0x00001ef4 main + 102
    22  taxi rider                          0x00001e85 start + 53
)
terminate called after throwing an instance of 'NSException'

Anyone have ideas?

+1  A: 

-dataUsingEncoding: is an NSString method.

According to the error message, the object you're sending it to is not an NSString, it's an NSArray.

If you are trying to convert an NSArray to NSData, see http://stackoverflow.com/questions/1286212/how-to-convert-nsarray-to-nsdata

Or, since it appears you're expecting to be able to read the NSData object with [CJSONDeserializer deserializer], you should probably create it with whatever the inverse of that is (is there a CJSONSerializer class in that package?).

David Gelhar
I use [CJSONDeserializer deserializer] to convert a json file to a dictionary, unfortunately the deserializer parsing only a small part of the json file, and create a dictionary containing a string and an array, i suppose must re-parsing the array
Kappe
p.s the json file is valid
Kappe