PDA

View Full Version : Can't detect NSString content because of Formatting




gwelmarten
Jun 8, 2012, 05:55 AM
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



robbieduncan
Jun 8, 2012, 06:51 AM
You cannot compare NSStrings with the == operator. This is comparing the pointer addresses, not the contents.

gnasher729
Jun 8, 2012, 06:55 AM
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

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.


You cannot compare NSStrings with the == operator. This is comparing the pointer addresses, not the contents.

Ashamed for not spotting this one.

chown33
Jun 8, 2012, 10:17 AM
Read the Object comparison (https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectComparison.html%23//apple_ref/doc/uid/TP40008195-CH37-SW1) article of Cocoa Core Competencies (https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/).

radiogoober
Jun 8, 2012, 01:35 PM
if ([ifYesMessage isEqualToString:@"yes"]) { }

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

Sydde
Jun 8, 2012, 09:25 PM
if ([ifYesMessage isEqualToString:@"yes"]) { }

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

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.

radiogoober
Jun 8, 2012, 09:50 PM
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.

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.

gnasher729
Jun 9, 2012, 02:56 AM
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.

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.

radiogoober
Jun 9, 2012, 06:22 AM
As a rule, there are seven replies to this thread and mine is the only helpful one.

gwelmarten
Jun 9, 2012, 06:25 AM
if ([ifYesMessage isEqualToString:@"yes"]) { }

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

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 :)

----------

As a rule, there are seven replies to this thread and mine is the only helpful one.

I completely agree!