Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C如何为不同的变量运行相同的代码_Objective C - Fatal编程技术网

Objective-C如何为不同的变量运行相同的代码

Objective-C如何为不同的变量运行相同的代码,objective-c,Objective C,我对编码相当陌生,我正在寻找一种方法来运行相同的代码,但每次都是针对不同的变量名 基本上,我有多个变量,每个变量都需要运行相同的代码。但是由于我希望应用程序工作的方式,我不想一次又一次地复制和粘贴代码并更改变量名称,因为这会使我的代码不整洁,如果我想进一步更改代码,我不希望每个变量都要编辑100行代码 例如: if (Variable1 == 1{ Object1.center.x = CGPointMake(AnotherObject1.center.x, AnotherO

我对编码相当陌生,我正在寻找一种方法来运行相同的代码,但每次都是针对不同的变量名

基本上,我有多个变量,每个变量都需要运行相同的代码。但是由于我希望应用程序工作的方式,我不想一次又一次地复制和粘贴代码并更改变量名称,因为这会使我的代码不整洁,如果我想进一步更改代码,我不希望每个变量都要编辑100行代码

例如:

   if (Variable1 == 1{
       Object1.center.x = CGPointMake(AnotherObject1.center.x, AnotherObject1.center.y);
       Random1.hidden = NO; 
       [More code here just don't need to type this out to explain my purpose]
   }
   if (Variable2 == 1{
       Object2.center.x = CGPointMake(AnotherObject2.center.x, AnotherObject2.center.y);
       Random2.hidden = NO; 
       [More code here just don't need to type this out to explain my purpose]
   }
   if (Variable3 == 1{
       Object3.center.x = CGPointMake(AnotherObject3.center.x, AnotherObject3.center.y);
       Random3.hidden = NO; 
       [More code here just don't need to type this out to explain my purpose]
   }
我需要20条if语句,你说它们几乎都运行相同的代码,但涉及不同的变量(在我的代码中,而不是if语句中),它们可以是从iBaction到碰撞的任何内容。我只是想用一个全局示例

我基本上需要把它缩短。我很高兴有重复的陈述,如果我需要改变一些影响主要陈述的东西,假设它们不会妨碍我


非常感谢您的帮助。

您可以创建带有参数的方法

-(void)methodName:(typeOfYourVariable)variable
{
here, write the code you want to apply to each variable.
}
并通过这样做调用此方法

[self methodName:variable1];
[self methodName:variable2];
[self methodName:variable3];
等等

这里有一个例子

- (void)viewDidLoad {
    [super viewDidLoad];
    int variable1 = 10;
    int variable2 = 15;
    int variable3 = 40;

    [self calculateSquare:variable1];
    [self calculateSquare:variable2];
    [self calculateSquare:variable3];

}

-(void)calculateSquare:(int)number
{
    NSLog(@"%i^2 = %i", number, number*number);
}
以下是控制台中的结果:

2014-11-29 00:30:49.743 Test a jeter[10134:418660] 10^2 = 100
2014-11-29 00:30:49.743 Test a jeter[10134:418660] 15^2 = 225
2014-11-29 00:30:49.743 Test a jeter[10134:418660] 40^2 = 1600
如果要使用多个参数:

- (void)viewDidLoad {
    [super viewDidLoad];
    int variable1 = 10;
    int variable2 = 15;
    int variable3 = 40;

    [self addNumber:variable1 andNumber:variable2];
    [self addNumber:variable1 andNumber:variable3];
    [self addNumber:variable2 andNumber:variable3];

}

-(void)addNumber:(int)number1 andNumber:(int)number2
{
    NSLog(@"%i+%i = %i", number1, number2, number1 + number2);
}
结果如下:

2014-11-29 09:54:04.470 Test a jeter[10395:466552] 10+15 = 25
2014-11-29 09:54:04.471 Test a jeter[10395:466552] 10+40 = 50
2014-11-29 09:54:04.471 Test a jeter[10395:466552] 15+40 = 55

当然,您可以使用任意数量的参数,两个或更多:)您不必使用相同类型的参数。你可以做你想做的^ ^

你需要做的就是编写一个内部过程并用SOAP(符号优化汇编程序)运行它。当然,唯一可以运行的系统可能是Smithsonian,我认为SOAP无论如何都不能识别Objective-C语法。(或者你可以编写一个方法来实现逻辑并多次调用它。)感谢+Atomnium只是一个简单的问题,我如何在方法中使用多变量更改(因此,不仅我选择的变量需要更改,我还需要更改2或3个其他变量)是否要使用多个参数?我将编辑我的答案;)谢谢!我唯一的问题是,我的布尔值没有一个用这种方法更新,我也不知道为什么。我用andBOOL:(BOOL)BoolenNameHere@Atomnium在方法名中声明它们