Mac Can't detect NSString content because of Formatting

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
477
0
England!
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
 

gnasher729

macrumors P6
Nov 25, 2005
16,692
3,383
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
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.
 

radiogoober

macrumors 6502a
Jun 7, 2011
972
1
if ([ifYesMessage isEqualToString:mad:"yes"]) { }

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

Sydde

macrumors 68020
Aug 17, 2009
2,120
3,147
IOKWARDI
if ([ifYesMessage isEqualToString:mad:"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

macrumors 6502a
Jun 7, 2011
972
1
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

macrumors P6
Nov 25, 2005
16,692
3,383
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.
 

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
477
0
England!
if ([ifYesMessage isEqualToString:mad:"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!
 

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.