1

我有代码能够检测用户何时没有完美运行的互联网但每次触发调试区域中的消息时都会显示:

检测到不正确的 NSStringEncoding 值 0x0000。假设 NSASCIIStringEncoding。将在不久的将来停止此兼容性映射。

如何摆脱此消息?

这是我正在使用的代码:

NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://google.co.uk"] encoding:NO error:nil];

if (connect == NULL) { 
//When there isn't internet
}
else {
//When there is
}

任何帮助,将不胜感激

4

2 回答 2

3

此行中调用的方法:

NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://google.co.uk"] encoding:NO error:nil];

期望字符串的编码方法,而不是 BOOL。

尝试这个:

   NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://google.co.uk"] encoding:NSUTF8StringEncoding error:nil];
于 2013-10-23T14:09:37.270 回答
0

您将NO(data type BOOL) 作为编码参数传递,它的类型为NSStringEncoding

方法签名是,

+ (instancetype)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

编码应该是其中之一,

enum {
   NSASCIIStringEncoding = 1,
   NSNEXTSTEPStringEncoding = 2,
   NSJapaneseEUCStringEncoding = 3,
   NSUTF8StringEncoding = 4,
   NSISOLatin1StringEncoding = 5,
   NSSymbolStringEncoding = 6,
   NSNonLossyASCIIStringEncoding = 7,
   NSShiftJISStringEncoding = 8,
   NSISOLatin2StringEncoding = 9,
   NSUnicodeStringEncoding = 10,
   NSWindowsCP1251StringEncoding = 11,
   NSWindowsCP1252StringEncoding = 12,
   NSWindowsCP1253StringEncoding = 13,
   NSWindowsCP1254StringEncoding = 14,
   NSWindowsCP1250StringEncoding = 15,
   NSISO2022JPStringEncoding = 21,
   NSMacOSRomanStringEncoding = 30,
   NSUTF16StringEncoding = NSUnicodeStringEncoding,
   NSUTF16BigEndianStringEncoding = 0x90000100,
   NSUTF16LittleEndianStringEncoding = 0x94000100,
   NSUTF32StringEncoding = 0x8c000100,
   NSUTF32BigEndianStringEncoding = 0x98000100,
   NSUTF32LittleEndianStringEncoding = 0x9c000100,
   NSProprietaryStringEncoding = 65536
};

前任:

NSError *error = nil;
NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://google.co.uk"] 
                              encoding:NSUTF8StringEncoding
                              error:&error];
if(error!=nil) {
    //Some error occured
}
于 2013-10-23T14:09:45.470 回答