Can't detect NSString content because of Formatting

Discussion in 'Mac Programming' started by gwelmarten, Jun 8, 2012.

  1. macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #1
    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:
    Code:
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You cannot compare NSStrings with the == operator. This is comparing the pointer addresses, not the contents.
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    You used NSLog to log whether the string contains "yes" or not. Wouldn't it be interesting to know what the string actually contains? Actually, from the output of your program you don't even know whether you were able to read any data from that URL at all. And I'd be curious how many characters there are in the string. Even if Safari shows "yes", it is quite possible that the number of characters is four or five.


    Ashamed for not spotting this one.
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
  5. macrumors 6502a

    Joined:
    Jun 7, 2011
    #5
    if ([ifYesMessage isEqualToString:mad:"yes"]) { }

    You have a lot of work ahead of you. I hope you read and read and read.
     
  6. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #6
    I sure would not trust that, I would be more inclined to use -caseInsensitiveCompare: to be safe. It is more words to type, but you only have to type them once.
     
  7. macrumors 6502a

    Joined:
    Jun 7, 2011
    #7
    Lol. My code words exactly fine for exactly what the OP was asking. Thank you for being the typical Internet user who has to "correct" something that was not incorrect. The OP did not ask about a case insensitive compare.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    As a rule, when you post your problems on the Mac Programming threads, you will get responses that help, not responses that answer what you ask about. And people who know about case insensitive compares wouldn't need to ask about it.
     
  9. macrumors 6502a

    Joined:
    Jun 7, 2011
    #9
    As a rule, there are seven replies to this thread and mine is the only helpful one.
     
  10. thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #10
    Hi Everyone
    Thanks for the help - yes - of course that was it. I had spent the last 3 days working on a section of code, and was a little dead when I always keep getting No returned (I omitted lots of the stuff inside the IF).

    What do you mean with lots of work? I can't think of any reason this would not work, and it does enable the entire program to work now :)

    ----------

    I completely agree!
     

Share This Page