Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Can't detect NSString content because of Formatting

gwelmartengwelmarten Posts: 15Registered Users
Hi
I'm trying to run an IF command based on the contents of an NSString from the contents of a URL. My current code is:
NSError* error1;
NSString *ifYesMessage = [NSString stringWithContentsOfURL:[NSURL URLWithString:@\"http://freedom-apps.com/branon/mac-desktop-pro/file.txt\"] encoding:NSASCIIStringEncoding error:&error1];
if (ifYesMessage == @\"yes\") {
NSLog(@\"Yes\");
}
else {
NSLog(@\"No\");
}


However, I always get No returned. Implying that the program can't find the yes printed in the text file. Does anybody know why this is? I thought it might be something to do with the encoding, but I've tried changing the encoding of the text file.
The text file literally says "yes". When you view source in Safari, it literally says "yes".

Thanks,
Sam
Post edited by gwelmarten on

Replies

  • givensurgivensur Posts: 314Registered Users
    edited June 2012
    First, isEqualToString: is how you compare a string. You do not use == to compare strings.

    NSString *foo = @\"bar\";

    if ([foo isEqualToString:@\"bar\"])
    NSLog(@\"The strings are the same\");
    else
    NSLog(@\"The strings are not the same\");


    Second, comparing whether or not two strings are equal is not the same thing as checking if one string contains another string. That'd be like expecting (59 == 6592) to return YES ;)

    Use rangeOfString: instead.


    NSString *myString = @\"The foo man group\";

    NSRange fooRange = [myString rangeOfString:@\"foo\"];
    if (fooRange.location != NSNotFound)
    NSLog(@\"myString contains foo\");
    else
    NSLog(@\"myString has no foo :(\");


    Also, when troubleshooting or debugging, don't just start trying things. Evaluate the problem first. For example you could've NSLogged your string you were creating to manually look to see if it contained "yes". Once seeing that you would realize that the encoding is not the problem.
    I make cake apps.
    Some of my code can be found on GitHub.
Sign In or Register to comment.