你没有提供足够的信息来给出一个模糊的答案,但你确实有一些选择。
最重要的是,你有一个“ error
”参数,你应该打印出它的结果。您还可以在 NSString 类中使用更好的 API。
将您的代码更改为以下内容:
NSError *error = NULL;
NSStringEncoding actualEncoding;
// variable names in Objective-C should usually start with lower case letters, so change
// URL in your code to "url", or even something more descriptive, like "urlOfOurString"
NSString *string = [[NSString alloc] initWithContentsOfURL:urlOfOurString usedEncoding:&actualEncoding error:&error];
if(string)
{
NSLog( @"hey, I actually got a result of %@", string);
if(actualEncoding != NSUTF8StringEncoding)
{
// I also suspect the string you're trying to load really isn't UTF8
NSLog( @"and look at that, the actual encoding wasn't NSUTF8StringEncoding");
}
} else {
NSLog( @"error when trying to fetch from URL %@ - %@", [urlOfOurString absoluteString], [error localizedDescription]);
}