Almost got it, just want to know this.

Discussion in 'iOS Programming' started by Abunga, Feb 1, 2011.

  1. macrumors newbie


    Jan 25, 2011
    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") {
    	} else {
  2. macrumors 604

    Aug 9, 2009
    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.
  3. macrumors 6502

    Aug 29, 2010
    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.

Share This Page