问题描述
无法弄清楚为什么这个错误会被抛出:
Can not figure out why this error keeps getting thrown:
-[__NSCFString bytes]: unrecognized selector sent to instance 0xc3eb200
:
- (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict {
empty = NO;
for (NSDictionary *valueDictionary in [dict objectForKey:@"Contacts"]) {
if ([[valueDictionary objectForKey:@"Empty"] isEqualToString:@"YES"]){
empty = YES;
contactsArray = [[NSMutableArray alloc] init];
}else{
Thecontacts = [valueDictionary objectForKey:@"Contacts"];
}
dataRepresentingSavedArray = Thecontacts;
if (dataRepresentingSavedArray != nil) {
**Causes Error->** NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
contactsArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
}
}
[table reloadData];
}
[valueDictionary objectForKey:@Contacts]的值是
The value for [valueDictionary objectForKey:@"Contacts"] is
<62706c69 73743030 d4010203 0405082b 2c542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748001 ab090a11 1718191a 2228292a 55246e75 6c6cd20b 0c0d0e56 24636c61 73735a4e 532e6f62 6a656374 738006a2 0f108002 8007d20b 0c0d1380 06a31415 16800380 0480055b 416e6472 65772044 756e6e5f 1018616e 64726577 4064756e 6e2d6361 72616261 6c692e63 6f6d5661 63636570 74d21b1c 1d215824 636c6173 7365735a 24636c61 73736e61 6d65a31e 1f205e4e 534d7574 61626c65 41727261 79574e53 41727261 79584e53 4f626a65 63745e4e 534d7574 61626c65 41727261 79d20b0c 0d248006 a3252627 80088009 800a5e4a 6f686e20 4170706c 65736565 645f1016 4a6f686e 2d417070 6c657365 6564406d 61632e63 6f6d5561 6c657274 12000186 a05f100f 4e534b65 79656441 72636869 76657200 08001100 16001f00 28003200 35003a00 3c004800 4e005300 5a006500 67006a00 6c006e00 73007500 79007b00 7d007f00 8b00a600 ad00b200 bb00c600 ca00d900 e100ea00 f900fe01 00010401 06010801 0a011901 32013801 3d000000 00000002 01000000 00000000 2d000000 00000000 00000000 00000001 4f>
我一直在使用代码,并且接近最终解决它是令人讨厌的,但它会工作直到我找到一个解决方法。 SO oldSavedArray打印数组,但是当我去打印时
I have been playing with the code and are close to final resolution it is nasty, but it will work until i find a work around. SO oldSavedArray prints the array but when i go to print this
contactsArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
我收到此信息。
[__NSCFString count]: unrecognized selector sent to instance 0xabab970
oldSavedArray的输出是
the output of oldSavedArray is
(
(
John Appleseed,
[email protected],
提醒
),
(
Andrew Dunn,
[email protected],
接受
)
)
( ( "John Appleseed", "[email protected]", alert ), ( "Andrew Dunn", "[email protected]", accept ))
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[AsyncURLConnection request:url completeBlock:^(NSData *data) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (![myString isEqualToString:@"0"]) {
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myString] forKey:@"savedArray"];
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"savedArray"]];
contactsArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
[table reloadData];
}else{
contactsArray = [[NSMutableArray alloc] init];
}
});
});
} errorBlock:^(NSError *errorss) {
}];
推荐答案
错误消息显示
Thecontacts = [valueDictionary objectForKey:@"Contacts"];
返回 NSString
对象,而不是 NSData
对象如您所料。我假设您使用类似
returns a NSString
object, not a NSData
object as you expected. I assume that you createdthe JSON using something like
[NSString stringWithFormat:@"%@", Contacts]
使用描述
方法 NSData
并返回一个字符串,如
which uses the description
method of NSData
and returns a string like
<62706c69 73743030 d4010203 0405082b ... >
或者SBJson隐含地这样做。你可以解析该字符串并将
转换回 NSData
。但这将是一个脆弱的方法,因为 NSData
的实际
描述
格式没有记录,可能会改变在将来。
or perhaps SBJson does that implicitly. You could parse that string and convertit back to NSData
. But that would be a fragile approach because the actualdescription
format of NSData
is not documented and might change in the future.
请注意,JSON不知道原始二进制数据,只知道字典,数组,字符串和数字。您应该将 NSData
转换为 NSString
(使用公开的
Base64转换器之一)并存储JSON中的Base64字符串。
Note that JSON does not know "raw binary data", only dictionaries, arrays, strings and numbers. You should convert the NSData
to NSString
(using one of the publicly availableBase64 converters) and store the Base64 string in the JSON.
这篇关于无法识别的选择器发送到NSKeyedUnarchiver的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!