0

is there an OR function in Objective-C?

for example:

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

|| doesnt seems to work.

Thanks.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Stefan
  • 235
  • 1
  • 5
  • 18
  • Do you get an error? What is it? `||` is a perfectly valid operator in Objective C. You can't compare strings with `==` though. See: http://stackoverflow.com/questions/1302985/how-do-i-compare-strings-in-objective-c – Chinmay Kanchi Aug 22 '10 at 17:13

2 Answers2

4

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

Try is code:

if([string1 isEqualToString:string2] || [string3 isEqualToString:string4]) 
{
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
1

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
  • 97,545
  • 26
  • 194
  • 236