Monday, April 11, 2011

BOOL changing value in objective-C?

I am having trouble with a BOOL property and I can't quite figure it out.

I have the following set in my .h file:

BOOL myVar;

@property BOOL myVar;

Then in my .m file I synthesize myVar and have a method that set myVar=YES; and also starts a timer. The timer then calls another method that tries to read the value of myVar.

To test for the value of myVar I have:

NSLog(@"The value of the bool is %@\n", (myVar ? @"YES" : @"NO"));

The first loop through the console shows myVar = YES then after that it says NO.

How do I keep it as the value of YES? Is it already YES but my NSLog is wrong? I need to test for YES/NO in that method because the actions taken will vary depending on their value.

From stackoverflow
  • Can you post a minimal set of code that demonstrates the problem?

    You can also look at manually writing out the setter for the property and placing a breakpoint in it. This will let you see if something else is changing the value.

  • Set a watch point on myVar to find out if it actually changes, with out seeing more source code we can't tell you what is going on :)

  • The reason it was failing was because I was comparing (myVar = YES) instead of (myVar == YES.)

    Yeah -- I am new.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.