Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C IF statement with OR condition

What's wrong in this IF statement?

if ([currentElement isEqualToString:@"aaa" || currentElement isEqualToString:@"bbb"])

XCode says:

No visible @interface for 'NSString' declares the selector 'isEqualToString:isEqualToString:'

I'm into an NSXML Parser procedure if it can help, but I think it's not that the problem.

like image 551
Alberto Schiariti Avatar asked Apr 17 '12 10:04

Alberto Schiariti


People also ask

How to write if condition in Objective-C?

For example, the following valid code: int x = 10; if (x > 10) x = 10; Essentially if the boolean expression evaluates to 1 (true) then the code in the body of the statement is executed (see Objective-C Operators and Expressions for more details of this type of logic).

Which is an if statement that is object of either IF or an ELSE *?

The if/else statement With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.

What is #if in Objective-C?

The syntax of an if statement in Objective-C programming language is − if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } If the boolean expression evaluates to true, then the block of code inside the if statement will be executed.

What is the syntax of if condition?

The syntax for if statement is as follows: if (condition) instruction; The condition evaluates to either true or false. True is always a non-zero value, and false is a value that contains zero.


1 Answers

You must compare result of two method calls:

if ([currentElement isEqualToString:@"aaa"] || [currentElement isEqualToString:@"bbb"])

The code you have actually compiles as

if ([currentElement isEqualToString:(@"aaa"||currentElement) isEqualToString:@"bbb"])

that is compiler tries to call non-existing isEqualToString:isEqualToString: method of NSString

like image 160
Vladimir Avatar answered Oct 20 '22 20:10

Vladimir