user440096
user440096

Reputation:

Having a problem with simple bool

I've some really simple code that checks if my bool is == YES but it does not ever enter.

NSLog(@"boool %d",self.arrayAlreadyPopulated );

if (self.arrayAlreadyPopulated == YES) 
{
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:(numMatchCounter)];
aMatch.teamName1 = TeamNameHolder;
}
else 
{

Match *aMatch = [[Match alloc] init];
aMatch.teamName1 = TeamNameHolder;
    [appDelegate.matchScoresArray addObject:aMatch];
[aMatch release];
}

The debug at the top says that the value of self.arrayAlreadyPopulated is 1 on the 2nd pass as it should be.

But it never enters the first first part but jumps down to the 'else'

I cant see for the life of me what the problem is. -.-

Anybody able to clue me in?

Thanks -Code

EDIT declaration code

BOOL arrayAlreadyPopulated;
@property (nonatomic) BOOL arrayAlreadyPopulated;

@synthesize arrayAlreadyPopulated;

Upvotes: 0

Views: 203

Answers (3)

Dhiraj Gupta
Dhiraj Gupta

Reputation: 10514

Check and double check that you're assigning the value to arrayAlreadyPopulated always as self.arrayAlreadyPopulated = YES instead of just arrayAlreadyPopulated = YES.

Sometimes, using the property v/s the associated variable of the property interchangeably doesn't always work out the way you'd expect it to. Use the "variable" name only if you're using it to release memory by [variable release] statement, just the way you'll find it in any Apple example code. In all other cases use self.propertyname.

Upvotes: 0

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Don't compare a BOOL against YES or NO. They can carry values that are not NO but don't compare equal to YES. Instead, use the boolean expression directly in the if statement:

if (self.arrayAlreadyPopulated)
{
    // ...
}

Upvotes: 2

Matthew Frederick
Matthew Frederick

Reputation: 22305

arrayAlreadyPopulated is probably not actually a BOOL. If, for example, it was a float, the %d would still print 1.

Upvotes: 1

Related Questions