【问题标题】:How to pass BOOL variable as parameter in Objective-C?如何在 Objective-C 中将 BOOL 变量作为参数传递?
【发布时间】:2016-02-15 13:24:40
【问题描述】:

这可能是个愚蠢的问题,但在我的应用程序中,需要将 bool 变量传递给方法。

假设我有 10 个 BOOL 变量声明为 b1,b2.....b10

我可以通过以下代码发送BOOL 值作为参数:

[self sendBoolValue:YES];      

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

现在我需要的是这样的东西:

[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.
}

我无法发送BOOL 变量。这有可能吗?

我为什么要这样做?:

我有一个 UIView,它在 3x3 网格布局中有 9 个子视图(我称之为瓦片)。

我有两个BOOLstartTileendTile。我需要根据触摸来设置这些值!!!

我正在使用touches-Began/Moved/Ended 来检测对这些视图的触摸

当触摸开始时,我需要计算触摸是在tile1还是tile2.....

所以实际代码:

- (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

}

如您所见,我需要调用相同的方法但需要发送三个不同的布尔变量!!!

【问题讨论】:

  • 您为什么要尝试使用 s 方法简单地将 BOOL 变量设置为 YES 或 NO?
  • 有一种情况是我经常将bool值从b1修改为b10。所以我写了一个方法来根据 b1...b10 的值进行一些改变。它包含一个有 10 个 case 的 switch statemnt。每个案例执行的工作都与触摸CGPOINT有关!!!!我需要在不同的点用不同的变量调用相同的方法!!!
  • 所以我没有使用多个方法,而是放置了一个方法,并且我想将变量传递给该方法
  • 您应该使用更相关的代码更新您的问题,并更好地解释您要完成的工作。
  • 我一定会的。是4!!我现在正赶着搭中转回家!!!我一到家就会这样做!!!

标签: ios objective-c boolean


【解决方案1】:

不是 100% 确定这是否是您想要的,但您可以做到:

[self sendBoolVariable:&b1];

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

【讨论】:

  • @Mr.T 这是我在问题下方的原始评论的重点。为什么你需要一个方法呢?此代码可以替换为b1 = YES;
  • 你说得对,代码可以换成b1=YES!!!但是在某些时候,我需要 b2=YES,广告在某些时候我需要 b3=YES,在某些时候我需要 b4=YES!!!!!!所以我需要将变量发送到这个方法并使它成为 YES!!其他代码行应该保持不变!!!! :) @rmaddy
【解决方案2】:

根据您的说明,如何:

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

BOOL 只是一个带符号的字符。 如果您尝试更改属性的值,请尝试 self.variable = YES 或 _variable = YES

也向barry wark 致敬:

来自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

【讨论】:

    【解决方案3】:

    当你将 BOOL b1 传递给方法时:

    [self sendBoolVariable:b1];
    

    “值”的范围仅限于方法。所以当你设置 value=YES 时:

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

    您没有更改更广泛的 ivar "b1" 的值。

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      相关资源
      最近更新 更多