如何将 NSDictionary 元素转换为 NSData (dataUsingEncoding 导致崩溃)

发布于 2024-09-26 04:20:31 字数 3403 浏览 3 评论 0原文

我尝试将 NSDictionary 元素转换为 NSData 元素:

...    
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] );
            }

        }
    }
...

但这样会崩溃:

    -[__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'

有人有想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-10-03 04:20:31

-dataUsingEncoding: 是一个 NSString 方法。

根据错误消息,您发送到的对象不是 NSString,而是 NSArray

如果您尝试将 NSArray 转换为 NSData,请参阅 如何将 NSArray 转换为 NSData?

或者,因为看起来您希望能够使用 [CJSONDeserializer deserializer]< 读取 NSData 对象/code>,您可能应该使用与它相反的任何内容来创建它(该包中有一个 CJSONSerializer 类吗?)。

-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 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?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文