0

I am trying to read the text from this file and display it in a label on my app. I am getting an error message with this piece of code.

NSURL *url = [NSURL URLWithString:@"http://www.funnynewsletter.tk/files/file.txt"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding];
label.text = content;

I know, it's simple. I'm just starting out learning iOS and I'm trying. If you could give me sources and ideas, not full code, that would be appreciated... I'm trying to learn, not just copy your work. Thanks everyone! :)

Error Message: No known class method for selector 'stringWithContentsOfUrl:encoding'

iDev
  • 23,310
  • 7
  • 60
  • 85
  • 1
    What is the error message? – SSteve Dec 20 '12 at 03:19
  • Oops. Sorry. :/ No known class method for selector 'stringWithContentsOfUrl:encoding' @SSteve Sorry if this is a stupid question... –  Dec 20 '12 at 03:21
  • No worries, everybody has to start somewhere. It might be that you're looking at older reference material. See my answer for details. – SSteve Dec 20 '12 at 03:27

5 Answers5

1

This line:

NSString *content = [NSString stringWithContentsOfURL:url 
                                             encoding:NSASCIIStringEncoding];

Should be this:

NSString *content = [NSString stringWithContentsOfURL:url 
                                             encoding:NSASCIIStringEncoding 
                                                error:nil];

If you still get problems, it is likely to be on the network.

update - error checking

You can check for errors like this:

NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url 
                                             encoding:NSASCIIStringEncoding 
                                                error:&error];
if (!content) {
    if (error) {
       NSLog (@"error %@",error);
      //handle error
    }
else {
   //do something with content
}

(fixed following Maddy's comment)

foundry
  • 31,615
  • 9
  • 90
  • 125
  • 1
    Passing `nil` for the error is not recommended. Error checking is important, especially when making network calls. – rmaddy Dec 20 '12 at 03:28
  • @maddy, I know (did you see my updated answer) - I just wanted to show the basic way so Henry can get going – foundry Dec 20 '12 at 03:36
  • I commented before your update as you can see from the timestamps. BTW - your updated code is incorrect. Never check the `error` variable first. Always check the return value of the method. In this case, check for `nil`. Only if the result is `nil` is it safe to check `error`. – rmaddy Dec 20 '12 at 03:39
  • @maddy - sorry - noticed the timestamp after posting comment! Thanks for your correction, I've updated my answer – foundry Dec 20 '12 at 03:44
1

The error message is telling you that the class NSString has no method of that name. The proper method is stringWithContentsOfURL:encoding:error:. I think there used to be stringWithContentsOfURL:encoding: but it was deprecated and then removed. If you're just testing and don't care about the error, you can pass nil for error:. But don't do that in any code that is at all serious.

SSteve
  • 10,550
  • 5
  • 46
  • 72
  • Thanks for the help. I know this isn't the best way to do this. Could you point me in a different direction to read a string from a server? @SSteve –  Dec 20 '12 at 03:31
  • 3
    @SSteve There is nothing wrong with your way ... except that it is synchronous/blocking. You can improve it by making it asynchronous, so the rest of your app doesn't have to wait for the result to be returned from the server. That's what Grand Central Dispatch is good for. – foundry Dec 20 '12 at 03:35
  • @HeWas: Thanks. The text from the file still isn't being displayed into the label. Any ideas? –  Dec 20 '12 at 03:39
  • Upload the file first. My browser can't find your file – iWheelBuy Dec 20 '12 at 03:44
  • My first step in debugging this problem would be to use NSLog to print `content`. I just tried that and there doesn't seem to be anything in that file. I got something from http://www.funnynewsletter.tk/file.txt, but probably a lot more than you want for a button label. – SSteve Dec 20 '12 at 03:47
  • @SSteve - Test the URL in your own browser... it's returning blank here. – foundry Dec 20 '12 at 03:47
0

Sometimes you just gotta read the spec. You're missing the "error:" parameter.

NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&errorMsg];
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0
    NSURL *url = [NSURL URLWithString:@"http://www.funnynewsletter.tk/files/file.txt"];

NSError *error;
NSString *urlContent = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

    if(error!=nil)
{ 


    NSLog(@"Error %@",error);

}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
VIP-DEV
  • 221
  • 1
  • 6
-1

If you want to get the string from a NSURL, description method can help.

NSURL *url = [NSURL URLWithString:@"http://www.funnynewsletter.tk/files/file.txt"];
label.text = url.description;
Y_York
  • 1
  • 1
    If all he wanted is the URL in the label, why would there be a need for `NSURL`? Obviously he wants the text result of loading the URL. – rmaddy Dec 20 '12 at 03:25