1

I have a label in a view which loads a plain text file and displays the content, but it displays weird characters not located in the .txt file along with the real content.

The text file has one line in it : "Update Submitted"

This is what it looks like on the actual device itself.

As you can see, the "Update Submitted" label has 2 weird characters in front of it.

I'm using this code :

content = [NSString stringWithContentsOfURL:[NSURL
URLWithString:@"http://samguichelaar.com/soadstatus.txt"] encoding: 1 error: NULL];
Label1.text = content;

Thanks in advance!

4

2 回答 2

2

设置编码 - NSUTF8StringEncoding

content = [NSString stringWithContentsOfURL:[NSURL
URLWithString:@"http://samguichelaar.com/soadstatus.txt"] encoding: NSUTF8StringEncoding error: NULL];
Label1.text = content;
于 2013-05-10T12:44:47.803 回答
2

您正在使用 ASCII 编码。切换到 UTF8 编码:

URLWithString:@"http://samguichelaar.com/soadstatus.txt"] encoding:NSUTF8StringEncoding error: NULL]

此外,您应该使用 Apple 为任何字符串编码提供的常量,以防 Apple 决定他们想要更改与任何常量关联的值:

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

搜索 NSStringEncoding

于 2013-05-10T12:45:52.857 回答