Alex Stelea
Alex Stelea

Reputation: 1219

checking if an textfield is a specific value

how can i check if a textfield contains a specific value i tried using the

if(x.text = @"hello") 

however this would work since it would always show me the alertiview i had below this code. I think i am missing something from my comparision however i am unsure.

Upvotes: 1

Views: 478

Answers (3)

DarkDust
DarkDust

Reputation: 92414

First of all, the code you've posted is an assignment (=), not a comparison (==). Then, what you need is ‘[x.text isEqual:@"hello"]‘. Otherwise you would be comparing pointers and they won't be equal.

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

You can use:

if ([x.text compare:@"hello"] == NSOrderedSame) {
    // NSString are equal!
}

Hope it helps.

Upvotes: 3

Vladimir
Vladimir

Reputation: 170849

  1. for compare in general you must use == operator, not an assignment operator =
  2. To compare strings you must use -isEqualToString: method as == operator will check if pointers to objects are equal, not the string values they contain.

So the correct code will be

if ([x.text isEqualToString:@"hello"])

Upvotes: 7

Related Questions