Ben Wilson
Ben Wilson

Reputation: 3137

Issue with if statement

I'm having an issue with an if statement

I have an object called enemy2 and I don’t want that object to run so I have used setHidden = YES and i was going to use the code

if(enemy2 setHidden: YES)
{

}
if(enemy2 setHidden: NO)
{

}

But its says its excepting ')' after the setHidden

Upvotes: 0

Views: 133

Answers (5)

Lorenzo B
Lorenzo B

Reputation: 33428

As jrturton suggested (also see my comment) you need to test against a bool value.

If I test the code (with LLVM Compiler)

if([enemy setHidden:YES]){

}

I receive a compile time error since [self setHidden:YES] returns a void.

Statement requires expression of scalar type ('void' invalid)

The correct approach could be the following, but it depends on what do you want to achieve.

if([enemy hidden]) {


}
else {


}

Edit

Based on Jonathan Grynspan report, if enemy is a subclass of UIView you should use isHidden instead of hidden since the getter in UIView class is defined as:

@property(nonatomic, getter=isHidden) BOOL hidden

Upvotes: 1

jrturton
jrturton

Reputation: 119292

Even if you correct the syntax, neither of these statements will probably execute. I think you want

if ([enemy2 hidden])
{
}
else
{
}

Using the getter, not the setter.

Upvotes: 4

san
san

Reputation: 3328

In objective-C you have to call method like this [anObject method]

So, ([enemy2 setHidden: YES]) instead of (enemy2 setHidden: NO)

Upvotes: 1

iArezki
iArezki

Reputation: 1271

if([enemy2 setHidden: YES])
{

}
if([enemy2 setHidden: NO])
{

}

Upvotes: 1

Ananth
Ananth

Reputation: 845

It should be as if([enemy2 setHidden: YES]) { } i.e inclue square brackets

Upvotes: 1

Related Questions