2

Can not figure out why this error keeps getting thrown:

-[__NSCFString bytes]: unrecognized selector sent to instance 0xc3eb200

for this code:

- (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];
}

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>

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];

i receive this.

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

the output of oldSavedArray is

( ( "John Appleseed", "John-Appleseed@mac.com", alert ), ( "Andrew Dunn", "andrew@me.com", 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) {

}];
Duny
  • 215
  • 4
  • 11

2 Answers2

5

The error message shows that

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

returns a NSString object, not a NSData object as you expected. I assume that you created the JSON using something like

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

which uses the description method of NSData and returns a string like

<62706c69 73743030 d4010203 0405082b ... >

or perhaps SBJson does that implicitly. You could parse that string and convert it back to NSData. But that would be a fragile approach because the actual description format of NSData is not documented and might change in the future.

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 available Base64 converters) and store the Base64 string in the JSON.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Not applicable for the case above but I got the exact same message when I omitted the external parameter name from init declaration. I am new to Objective C/Swift world so the concept of different parameter name externally and internally is novel still and easy to overlook, I am posting here as a convenience to those who search for the title of this post, as I did.

 //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