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

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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];

	}
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
== compares pointers, not the strings themselves. You want:

Code:
[myTitle isEqualToString:@"HELLO"]

which returns true if the strings are equal.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Thanks you guys :) I guess Im still stuck in the world of C++/C#. Wow, objc is VERY different..:confused:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.