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

Abunga

macrumors newbie
Original poster
Jan 25, 2011
15
0
Hello again, my knowledge is rapidly improving but i have come stuck with this problem.

I have created a small app, just for practice, that will compare the answer given my a user in a text field with a set word. This is for a single on screen anagram. Should the user put in 'LITTLE', it will say WRONG! and should they put in 'DANGEROUS', it will of course say CORRECT! My issue is that I have tried different formations to no avail. It wont use the 'compare' operation with a text field.

What might you suggest. The offending code is below.

Thanks, Abunga.

Code:
#import "Text_PracticeViewController.h"

@implementation Text_PracticeViewController


@synthesize userInput;
@synthesize userMark;
@synthesize userCorrect;

-(IBAction) setOutput:(id)sender {
	if (userInput=@"DANGEROUS") {
		userCorrect.text=@"CORRECT!";
	} else {
		userCorrect.text=@"WRONG!";
}
		
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,731
8,407
A sea of green
First, = is not a compare operator. == is the equality comparison.

Second, never use == to compare strings for equality. There is an NSString method for this purpose. Consult the NSString reference doc, and search for the term equal. You should see several methods. Read the descriptions before choosing the correct one.

Third, since you didn't show what type userInput is, you may have to use one of its properties that is an NSString, rather than the userInput variable itself. We could guess that userInput is some kind of UI text field, but we'd just be guessing, which is a poor way to debug anything.
 

cnstoll

macrumors 6502
Aug 29, 2010
254
0
Just a further note.

The reason you never use "==" to compare Objects (such as an instance of NSString, or UITextField) is that "==" will compare the values of the pointers, not the values the Objects contain. Always use comparison methods to compare the values of Objects.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.