0

我正在启动一个非常简单的学习应用程序,我正在使用 NSURLConnection 并开始研究读取文件的内容,例如 www.funnynewsletter.tk/files/files.txt 并将它们作为字符串读取到 Objective C 中。

我想知道如何执行此操作并将保存的字符串打印到标签“标签”上。

谁能帮我吗?

这是我当前的代码。

- (void) viewDidUnLoad
{
NSError* e;
NSString *str = [NSString stringWithContentsOfURL:@"http://funnynewsletter.tk/files/files.txt"];
if(e != nil) {
    NSLog(@"Error");
}
label.text = self.str;
}

这在第 4 行和第 8 行有错误。我不知道发生了什么。任何人都可以帮忙吗?

4

2 回答 2

2

您正在发送NSString而不是NSURL.

- (void) viewDidLoad
    {
    //NSError* e;
    NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://funnynewsletter.tk/files/files.txt"]];
    //if(e != nil) {
    //    NSLog(@"Error");
    //}
    label.text = str;
    }

试试这个...

于 2012-11-29T04:38:52.837 回答
0

使用此方法

+ (id)stringWithContentsOfFile:(NSString *)path
                  usedEncoding:(NSStringEncoding *)enc
                         error:(NSError **)error

您正在使用的那个已被弃用,并且没有编码、错误支持。

- (void) viewDidUnLoad
{
   NSError* e;
   NSString *str = [NSString stringWithContentsOfFile:@"http://funnynewsletter.tk/files/files.txt"
                    usedEncoding:(NSASCIIStringEncoding   //OR NSUTF8StringEncoding if utf8 not ASCII
                    error:&e];
if(e != nil) {
    NSLog(@"Error");
}
else
   label.text =str;
}
于 2012-11-29T04:42:55.403 回答