views:

37

answers:

3

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.

+7  A: 
  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"])
Vladimir
+3  A: 

You can use:

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

Hope it helps.

Pablo Santa Cruz
A: 

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.

DarkDust