Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Ive got a little codesnippet where Im trying to compare a string with another.

Even though writing out the myTitle-variable in the alertbox will in fact display the string ("HELLO") that Im comparing it with (tried it by removing the if-statement and just executing the alert-box), the if-statement wont equate myTitle to the string.

Is this due to some inconsistency between datatypes, perhaps? I mean, they are both strings, arent they, and thus should be comparable?

Code:
	NSString *myTitle = level2ViewController.title;
	if(myTitle == @"HELLO")
	{
			// Display alert
			UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Title:" message:myTitle delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
			[myAlert show];
			[myAlert release];

	}
 
== compares pointers, not the strings themselves. You want:

Code:
[myTitle isEqualToString:@"HELLO"]

which returns true if the strings are equal.
 
The same is true of all objects not just strings. You cannot compare the contents using '==' you must use an isEqualToxxxx: method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.