PDA

View Full Version : Almost got it, just want to know this.




Abunga
Feb 1, 2011, 12:06 PM
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.



#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
Feb 1, 2011, 12:14 PM
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
Feb 1, 2011, 03:09 PM
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.