0

Convert __NSCFString to NSData

App crash when I try to get data from my NSDictionary

NSData *routeData = [dictSelectedScheduleTrip valueForKey:@"routeSerialize"];

Crash Log [__NSCFString bytes]: unrecognized selector sent to instance 0x107310000

Console Code

po [[dictSelectedScheduleTrip valueForKey:@"routeSerialize"] class]

__NSCFString

Below is my NSData (Not added whole data because data is too long if you want to check all data please open below link) NSData

routeSerialize = "<d5112fa3 0c000000 382e3330 2e313031 2e313536 02000000 30000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00030200 00000710 008e015d 474cc1fe 5e7fa094 477dd52f 5f0b0400 9f000000 0c240003 01000402 04000200 00000104 00000000 00030100 04020400 02000000 01040000 0000000e 00000600 00000000>";

Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
  • Does this answer your question? [Converting HEX NSString To NSData](https://stackoverflow.com/questions/7317860/converting-hex-nsstring-to-nsdata) – Larme Nov 28 '19 at 11:56
  • Question is: Why do you have output of NSData description as the value? Because since iOS 13, it doesn't print the same thing, so you'll have an issue! There is a critical bad architecture that highly risk to create future issues – Larme Nov 28 '19 at 11:58
  • @Larme, Thank you for your interest in my question, but We are store NSData as a `BLOB` in MYSQL DB, When I Received the data is something like that which is mentioned in above question, If you have any other way to convert string as NSData, Please let me know I'll follow as per your answer. Right now I don't know any other solution for my problem, That's why I follow @zaph answer. – Vivek Nov 29 '19 at 08:46
  • It's not about the NSString to NSData, it's about how you saved that Data and transformed it into a NSString. I'm afraid that if you run your project compiled with XCode 11 on an iOS13 device you'll have issue, because routeSerialize will not look like that at all. – Larme Nov 29 '19 at 08:47
  • as per the HERE map document, I'll follow the serialization route with this method. https://developer.here.com/documentation/ios-premium/dev_guide/topics/route-serialization.html – Vivek Nov 29 '19 at 08:50
  • I'd say that your issue is about saving saving the serialization route in your database. – Larme Dec 02 '19 at 11:57

2 Answers2

0

I found a solution.

Original Answer @zaph

+ (NSData *)dataFromHexString:(NSString *)string
{
    string = [string lowercaseString];
    NSMutableData *data= [NSMutableData new];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i = 0;
    int length = string.length;
    while (i < length-1) {
        char c = [string characterAtIndex:i++];
        if (c < '0' || (c > '9' && c < 'a') || c > 'f')
            continue;
        byte_chars[0] = c;
        byte_chars[1] = [string characterAtIndex:i++];
        whole_byte = strtol(byte_chars, NULL, 16);
        [data appendBytes:&whole_byte length:1];
    }
    return data;
}
Vivek
  • 4,916
  • 35
  • 40
0

__NSCFString is a class cluster of NSString, you could convert it as is NSString *, no print (call - (NSString *)description) to get hex string needed.

NSData *routeData = [dictSelectedScheduleTrip valueForKey:@"routeSerialize"];

if ([routeData isKindOfClass:NSString.class]) {
    routeData = [(NSString *)routeData dataUsingEncoding:NSUTF8StringEncoding]; // or your other prefer data encoding.
}
Itachi
  • 5,777
  • 2
  • 37
  • 69
  • Do not use `-description` to get the hex data. This is not a promised interface, and in fact was changed in iOS 13, breaking many apps that relied on it. See https://stackoverflow.com/questions/1305225/best-way-to-serialize-an-nsdata-into-a-hexadeximal-string for one of several converters. – Rob Napier Dec 05 '19 at 13:44