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.
- for compare in general you must use
==
operator, not an assignment operator=
- 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.
精彩评论