NSString initWithData 返回 nil

发布于 2024-11-17 19:29:49 字数 952 浏览 6 评论 0原文

我与服务器一起工作。我向服务器发送请求,它以 UTF-8 编码回答我,但是当我尝试将字节数组解码为字符串时,有时我会得到 nil 值。如何解码这个字节数组而不出现任何错误?

- (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);
    //...
}

当我使用 NSUTF8StringEncoding 进行转换时,我得到如下内容:

xt":"","newstype":[{"id":"1","name":"\u043f\u0440\u043e\u0438\u0441\u0448\u0435\u0441\u0442\u0432\u0438 \u0435","ic

当我使用转换时NSNonLossyASCIIStringEncoding 我得到这样的信息:

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

但有时,使用 NSNonLossyASCIIStringEncoding 进行转换时,我会收到错误。我不知道为什么

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":"\u043f\u0440\u043e\u0438\u0441\u0448\u0435\u0441\u0442\u0432\u0438\u0435","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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云柯 2024-11-24 19:29:49

您说您的服务器发送以 UTF-8 编码的数据,但在您的代码中您使用 ASCII 对其进行解码。
将编码设置为 NSUTF8StringEncoding。

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.

是伱的 2024-11-24 19:29:49

我认为您可能需要将终止“0”附加到您的数据中。

来自我的项目之一:

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

   // etc your usual code here
}

然后使用正确的编码照常执行。

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.

肤浅与狂妄 2024-11-24 19:29:49

我不知道为什么会发生这个错误。我是这样修复的:

-(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 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;
}
平定天下 2024-11-24 19:29:49

我在使用 RestKit 时遇到了同样的问题。方法 [response bodyToString] 不适用于西里尔字母编码。我通过切换到 NSWindowsCP1251StringEncoding 解决了这个问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文