Xcode 5 - 检查用户是否具有Internet连接 - 检测到错误的NSStringEncoding值0x0000

时间:2013-10-23 14:04:53

标签: ios iphone objective-c xcode

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

  

检测到错误的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
}

任何帮助将不胜感激

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

您正在将NO(数据类型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
}