2

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

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

我一直在玩代码并且接近最终分辨率它很讨厌,但它会工作,直到我找到解决方法。所以 oldSavedArray 打印数组但是当我去打印这个

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

我收到这个。

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

oldSavedArray 的输出是

((“John Appleseed”,“John-Appleseed@mac.com”,警报),(“Andrew Dunn”,“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) {

}];
4

2 回答 2

5

错误信息显示

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

返回一个NSString对象,而不是NSData您预期的对象。我假设您使用类似的东西创建了 JSON

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

它使用descriptionof 的方法NSData并返回一个类似的字符串

<62706c69 73743030 d4010203 0405082b ... >

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

请注意,JSON 不知道“原始二进制数据”,只知道字典、数组、字符串和数字。您应该将其转换NSDataNSString(使用公开可用的 Base64 转换器之一)并将 Base64 字符串存储在 JSON 中。

于 2013-07-08T05:26:20.567 回答
0

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

 //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
于 2015-06-20T20:29:42.363 回答