无法识别的选择器发送到NSKeyedUnarchiver的实例

时间:2013-07-08 05:15:26

标签: ios objective-c xcode

无法弄清楚为什么这个错误会被抛出:

-[__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”]的值为

<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会打印数组,但是当我打印这个

 contactsArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];

我收到了这个。

   [__NSCFString count]: unrecognized selector sent to instance 0xabab970

oldSavedArray的输出是

(         (         “John Appleseed”,         “John-Appleseed@mac.com”         警报     )         (         “安德鲁邓恩”,         “andrew@me.com”         接受     ) )

 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) {

}];

2 个答案:

答案 0 :(得分:5)

错误消息显示

Thecontacts = [valueDictionary objectForKey:@"Contacts"];

返回NSString对象,而不是预期的NSData对象。我假设你创造了 使用类似

的JSON
[NSString stringWithFormat:@"%@", Contacts]

使用description的{​​{1}}方法并返回类似

的字符串
<62706c69 73743030 d4010203 0405082b ... >

或者也许SBJson隐含地这样做了。您可以解析该字符串并进行转换 它回到NSData。但这是一种脆弱的方法,因为实际的 NSData description格式未记录在案,可能会在将来发生变化。

请注意,JSON不知道&#34;原始二进制数据&#34;,只有字典,数组,字符串和数字。您应该将NSData转换为NSData(使用其中一个公开的 Base64转换器)并将Base64字符串存储在JSON中。

答案 1 :(得分:0)

不适用于上述情况,但是当我从init声明中省略了外部参数名称时,我收到了完全相同的消息。我是Objective C / Swift世界的新手,所以外部和内部不同参数名称的概念仍然是新颖的,容易被忽视,我在这里发帖是为了方便那些搜索这篇文章标题的人,就像我一样。 / p>

 //Non working code, compiles but throws unrecognized selector exception message when unarchiver is called. Programmer's error.
     required init(decoder: NSCoder) {
    //...... unarchiving code goes here.
    }


    //Working code
    required init(coder decoder: NSCoder) {

    //Same unarchiving code went here.
    }

    //Client code, same in both cases
let mArchiveName = NSKeyedArchiver.archivedDataWithRootObject(origMyObject)
let restoredMyObject = NSKeyedUnarchiver.unarchiveObjectWithData(mArchiveName) as! MyObject