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 に答える