Teja Nandamuri
Teja Nandamuri

Reputation: 11201

How to pass BOOL variable as parameter in Objective-C?

This might be a stupid question, but in my app there will be a need to pass a bool variable to a method.

Let's say I have 10 BOOL variables declared as b1,b2.....b10.

I can send BOOL value as a parameter by simply with the following code:

[self sendBoolValue:YES];      

- (void)sendBoolValue:(BOOL)value 
{
    b1 = value;
    // now b1 will be YES.
}

Now what I need is something which does this:

[self sendBoolVariable:b1];  // I tried sending &b1, but it didnt work out. 

- (void)sendBoolVariable:(BOOL)value
{
    value = YES; // trying to set b1 to YES.
    // b1 is still NO.
}

I couldnt get to send the BOOL variable. IS this even possible ?

Why I am doing this?:

I have a UIView which has 9 subviews(I call these as tiles) in a 3x3 grid layout.

I have two BOOL values startTile and endTile. I need to set these values based on the touch!!!

I am using touches-Began/Moved/Ended to detect touches on these views

When the touches started, I need to calculate if the touch is in the tile1 or tile2.....

So the actual code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// calculate touch point and based on it set the bool value
    [self sendBoolVariable:startTile];
  //startTile is selected, so change its color
  // lock other tiles    

}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

  //if touches came to tile 2 region
  [self sendBoolVariable:b2];   //b2 is BOOL variable for tile2 



}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self sendBoolVariable:endTile]; 

    //end tile is selcted too
     //at this point both start tile and tile are selected
     //now do the animation for the start tile and end tile
     //other tiles are still in locked state

}

As you can see, I need to call same method but need to send three different bool variables!!!!

Upvotes: 6

Views: 9470

Answers (3)

jsondwyer
jsondwyer

Reputation: 437

When you pass the BOOL b1 to the method:

[self sendBoolVariable:b1];

"value" has a scope limited to the method. So when you set value=YES:

-(void)sendBoolVariable:(BOOL) value{
         value=YES;

you are not changing the value of the broader scope ivar "b1".

Upvotes: 0

Roy Falk
Roy Falk

Reputation: 1741

Following your clarifications, how about:

BOOL a = YES;
a = [self modifyBoolValueBasedOnItself:a]; // the method takes the BOOL as a parameter and returns the modified value 

An BOOL is just a a signed char. If you're trying to change the value of a property, try self.variable = YES or _variable = YES

Also kudos to barry wark for the following:

From the definition in objc.h:

typedef signed char     BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.
#define OBJC_BOOL_DEFINED
#define YES             (BOOL)1
#define NO              (BOOL)0

Upvotes: 0

dan
dan

Reputation: 9825

Not 100% sure if this is what you want but you can do:

[self sendBoolVariable:&b1];

- (void)sendBoolVariable:(BOOL *)value {
    *value = YES; //b1 is now YES        
}

Upvotes: 7

Related Questions