【发布时间】:2010-10-05 09:42:46
【问题描述】:
所以我有这段代码:
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 为真时,receivedPage 变为空。为什么会这样?
接收到的页面就是这个函数的返回值:
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(不知道为什么,我在调试别人的代码)。
知道为什么会这样吗?
【问题讨论】: