重新编码 NSString 返回 null

发布于 2024-09-26 00:02:02 字数 1035 浏览 0 评论 0原文

所以我有这段代码:

if ([receivedPage hasPrefix:[NSString stringWithUTF8String:"\xC3\xAF\xC2\xBB\xC2\xBF"]]) // UTF-8 BOM 'EF BB BF' as UTF-16 chars
    {
        //DebugLog(@"converting calls list to UTF8");
        receivedPage = [[[NSString alloc] initWithData:[receivedPage dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] autorelease];
    }

但是有时当 if 为 true 时,receedPage 变为 null。为什么会发生这种情况?

接收到的页面就是这个函数的返回值:

 NSURLResponse * response;
    NSData * result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

    if ([result length] > 0)
        return [[[NSString alloc] initWithBytes: (const void*)[result bytes] length:[result length] encoding: encoding] autorelease];
    else
    {
        if (error && *error)
            DebugLog(@"URL request got error: %@",*error);
        return nil;
    }

这里的编码是NSISOLatin1StringEncoding(不知道为什么,我正在调试别人的代码)。

知道为什么会发生这种情况吗?

So I have this piece of code :

if ([receivedPage hasPrefix:[NSString stringWithUTF8String:"\xC3\xAF\xC2\xBB\xC2\xBF"]]) // UTF-8 BOM 'EF BB BF' as UTF-16 chars
    {
        //DebugLog(@"converting calls list to UTF8");
        receivedPage = [[[NSString alloc] initWithData:[receivedPage dataUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] autorelease];
    }

However sometimes when the if is true the receivedPage becomes null. why would this happen?

The received page is the returned value of this function:

 NSURLResponse * response;
    NSData * result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

    if ([result length] > 0)
        return [[[NSString alloc] initWithBytes: (const void*)[result bytes] length:[result length] encoding: encoding] autorelease];
    else
    {
        if (error && *error)
            DebugLog(@"URL request got error: %@",*error);
        return nil;
    }

The encoding here is NSISOLatin1StringEncoding (don't know why ,I'm debugging someone else's code).

Any idea why this would happen?

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

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

发布评论

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

评论(1

樱花落人离去 2024-10-03 00:02:02

看起来您正在尝试将字符串(包含字符的对象)视为数据(包含字节的对象)。保留从连接收到的数据,并检查其中的 UTF-8 BOM(正确的三字节版本),然后根据情况使用 NSUTF8StringEncodingNSISOLatin1StringEncoding无论你找到它。

或者,如果您也可以修复服务器来执行此操作,则有条件地使用 UTF-8。

另外,您可能应该切换此代码以异步使用 NSURLConnection。如果用户的互联网连接速度很慢,您的应用程序就会挂在这里。通过异步执行此操作,您可以保持 UI 运行,在适当的情况下显示进度,并允许用户取消。

It looks like you're trying to treat strings (objects containing characters) as data (objects containing bytes). Keep the data you received from the connection, and check for the UTF-8 BOM (the proper three-byte version) in it, then use either NSUTF8StringEncoding or NSISOLatin1StringEncoding based on whether you find it.

Or, just use UTF-8 conditionally, if you can fix the server to do that as well.

Also, you should probably switch this code to use the NSURLConnection asynchronously. If the user's internet connection is slow, you're hanging your app here. Doing it asynchronously lets you keep the UI running, display progress if appropriate, and enable the user to cancel.

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