gpt4 book ai didi

ios - 如何在 Objective-C 中将 BOOL 变量作为参数传递?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:25:25 31 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但在我的应用程序中需要将 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 个 subview (我称它们为图 block )。

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

我正在使用 touches-Began/Moved/Ended 来检测这些 View 上的触摸

当触摸开始时,我需要计算触摸是在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

}

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

最佳答案

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

[self sendBoolVariable:&b1];

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

关于ios - 如何在 Objective-C 中将 BOOL 变量作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701622/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com