views:

84

answers:

2

Hi, is there an OR function in Objective-C?

for example:

if(string1 == string2 || string3 == string 4)

|| doesnt seems to work.

Thanks.

+4  A: 

|| is a valid Objectivc-C and C Operator. It should work.

Try is code:

if([string1 isEqualToString:string2] || [string3 isEqualToString:string4]) 
{
}
Henrik P. Hessel
Too many parentheses!
jtbandes
Yap, bad habit :)
Henrik P. Hessel
sorry for editing but naming a string `string` could have been misleading :-)
klez
+1  A: 

The or-operator || works fine, but the == operator doesn't work for C-strings nor for NSString if you want to test for string-equality. As written you only test for the pointers being equal.

Instead use:

  • strcmp() for C-style strings
  • -isEqualToString: for NSString
Georg Fritzsche
The only issue here is strcmp will return 0 if the strings are equal, you'd have to invert the checks.
Joshua Weinberg
No reason to be using strcmp anyway.
ZaBlanc
@ZaBlanc: Could you be more specific?
Georg Fritzsche