将特殊字符(NSString)转换为NSData的Xcode



可能重复:
如何从包含国际字符的NSString中准备NSURL?

我遇到了将特殊字符转换为NSData格式的问题。

NSString *strURL = [NSString stringWithFormat:@"http://www.???.com/1.php?test=你好"];
NSLog(@"%@", strURL);
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSLog(@"%@", dataURL);

当我打印出dataURL时,结果是(null(,而当我的strURL是英文时,它会打印出应该打印的正确数据。

请告知。

您应该尝试将UTF-8添加到字符串中

[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

最新更新