如何将 NSDictionary 元素转换为 NSData (dataUsingEncoding 导致崩溃)
我尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-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
toNSData
, 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 aCJSONSerializer
class in that package?).