读取URL内容XCode sigabrt错误

时间:2013-07-12 11:50:44

标签: ios objective-c sigabrt

这是我的代码,它给出了"Program received signal "SIGABRT".
我是iOS发展的新人。

- (IBAction)getData {

    NSURL *URL = @"http://www.oeslabs.com/aa.txt";

    NSData *data = [NSData dataWithContentsOfURL:URL];

    NSString *string = [NSString stringWithUTF8String:[data bytes]];    
    if (string == nil) {

        // an error occurred
        label1.text =@"Error";
    }
    else{
        label1.text = string;
    }
}

2 个答案:

答案 0 :(得分:2)

您也可以将代码设为

NSString *urlString = [NSString stringWithFormat:@"http://www.oeslabs.com/aa.txt"];
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
.
.
.
.
.

以下是stringByAddingPercentEscapesUsingEncoding.用于转换合法URL字符串的官方文档。

答案 1 :(得分:1)

您正在尝试分配NSString  到NSURL

替换

NSURL *URL = @"http://www.oeslabs.com/aa.txt";

NSURL *URL = [NSURL URLWithString:@"http://www.oeslabs.com/aa.txt"];