简体   繁体   中英

NSString initWithData returns nil

I work with server. I send request to the server and it answers me in UTF-8 encoding, but when I try to decode byte array to the string, sometimes I get nil value. How can I decode this bytes array without any errors?

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString * result_string=[[[NSString alloc] initWithData:receivedData encoding:NSNonLossyASCIIStringEncoding] autorelease];
    NSLog(result_string);
    //...
}

when I convert using NSUTF8StringEncoding I get something like this:

xt":"","newstype":[{"id":"1","name":"\п\р\о\и\с\ш\е\с\т\в\и\е","ic

when I convert using NSNonLossyASCIIStringEncoding I get something like this:

xt":"","newstype":[{"id":"1","name":"происшествие","ic

but sometimes, converting using NSNonLossyASCIIStringEncoding , I get errors. I don't know why

You say that your server sends data encoded in UTF-8, but in your code you're decoding it with ASCII. Set encoding to NSUTF8StringEncoding.

I think you might need to append the terminating "0" to your data.

From one of my projects:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   char temp[] = {0};
   [receivedData appendBytes:temp length:1];

   // etc your usual code here
}

Then perform as usual with the correct encoding.

I don't know why this error happened. I fixed it like this:

-(id)init{
    self=[super init];
    if(self){
        NSArray *codes=[NSArray arrayWithObjects:@"\\u0410",@"\\u0411",@"\\u0412",@"\\u0413",@"\\u0414",@"\\u0415",@"\\u0416",@"\\u0417",@"\\u0418",@"\\u0419",@"\\u041a",@"\\u041b",@"\\u041c",@"\\u041d",@"\\u041e",@"\\u041f",
                        @"\\u0420",@"\\u0421",@"\\u0422",@"\\u0423",@"\\u0424",@"\\u0425",@"\\u0426",@"\\u0427",@"\\u0428",@"\\u0429",@"\\u042a",@"\\u042b",@"\\u042c",@"\\u042d",@"\\u042e",@"\\u042f",
                        @"\\u0430",@"\\u0431",@"\\u0432",@"\\u0433",@"\\u0434",@"\\u0435",@"\\u0436",@"\\u0437",@"\\u0438",@"\\u0439",@"\\u043a",@"\\u043b",@"\\u043c",@"\\u043d",@"\\u043e",@"\\u043f",
                        @"\\u0440",@"\\u0441",@"\\u0442",@"\\u0443",@"\\u0444",@"\\u0445",@"\\u0446",@"\\u0447",@"\\u0448",@"\\u0449",@"\\u044a",@"\\u044b",@"\\u044c",@"\\u044d",@"\\u044e",@"\\u044f",
                        @"\\u0401",@"\\u0451",@"\\u00a0",nil];
        NSArray *res=[NSArray arrayWithObjects:@"А",@"Б",@"В",@"Г",@"Д",@"Е",@"Ж",@"З",@"И",@"Й",@"К",@"Л",@"М",@"Н",@"О",@"П",
                                               @"Р",@"С",@"Т",@"У",@"Ф",@"Х",@"Ц",@"Ч",@"Ш",@"Щ",@"Ъ",@"Ы",@"Ь",@"Э",@"Ю",@"Я",
                                               @"а",@"б",@"в",@"г",@"д",@"е",@"ж",@"з",@"и",@"й",@"к",@"л",@"м",@"н",@"о",@"п",
                                               @"р",@"с",@"т",@"у",@"ф",@"х",@"ц",@"ч",@"ш",@"щ",@"ъ",@"ы",@"ь",@"э",@"ю",@"я",
                                               @"Ё",@"ё",@" ",nil];
        dic=[[NSMutableDictionary alloc] initWithObjects:res forKeys:codes];
    }
    return self;
}

-(NSString*)convertToString:(NSString*)str{
    if(str==nil)
        return nil;
    NSString *new_str=str;
    for (NSString *s in [dic allKeys]) {
        new_str=[new_str stringByReplacingOccurrencesOfString:s withString:[dic objectForKey:s]];
    }
    return new_str;
}

I faced the same problem while using RestKit. Method [response bodyToString] was not working with cyrillic encoding. I solved it by switched to NSWindowsCP1251StringEncoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM