开发者

checking if an textfield is a specific value

开发者 https://www.devze.com 2023-01-19 18:07 出处:网络
how can i check if a textfield开发者_运维百科 contains a specific value i tried using the if(x.text = @\"hello\")

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.


  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"])


You can use:

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

Hope it helps.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消