在目标C中通过值或引用传递参数 [英] Passing arguments by value or by reference in objective C

查看:83
本文介绍了在目标C中通过值或引用传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对目标c有点陌生,我正在尝试通过引用传递一个参数,但是行为好像是一个值.你知道为什么这行不通吗?

I'm kind of new with objective c and I'm trying to pass an argument by reference but is behaving like it were a value. Do you know why this doesn't work?

这是功能:

- (void) checkRedColorText:(UILabel *)labelToChange {
    NSComparisonResult startLaterThanEnd = [startDate compare:endDate];
    if (startLaterThanEnd == NSOrderedDescending){
        labelToChange.textColor = [UIColor redColor];
    }
    else{
        labelToChange.textColor = [UIColor blackColor];
    }

}

这是电话:

UILabel *startHourLabel; // This is properly initialized in other part of the code
[self checkRedColorText:startHourLabel];

感谢您的帮助

推荐答案

仅Objective-C 支持按值传递参数.这里的问题可能已经得到解决(因为这个问题已有一年多的历史了,但我需要澄清一些有关参数和Objective-C的问题.

Objective-C only support passing parameters by value. The problem here has probably been fixed already (Since this question is more than a year old) but I need to clarify some things regarding arguments and Objective-C.

Objective-C 是严格超集表示C所做的所有事情,Obj-C也一样.

Objective-C is a strict superset of C which means that everything C does, Obj-C does it too.

通过快速浏览Wikipedia,您可以看到 Function parameters are always passed by value

By having a quick look at Wikipedia, you can see that Function parameters are always passed by value

Objective-C没什么不同.这里发生的事情是,每当我们将对象传递给函数(在本例中为UILabel *)时,我们都会在指针的地址处传递值 contained .

Objective-C is no different. What's happening here is that whenever we are passing an object to a function (In this case a UILabel *), we pass the value contained at the pointer's address.

无论您做什么,都将永远是您传递的价值.如果要传递 reference 的值,则必须将其传递给**对象(就像传递NSError时经常看到的那样).

Whatever you do, it will always be the value of what you are passing. If you want to pass the value of the reference you would have to pass it a **object (Like often seen when passing NSError).

这与标量相同,它们按值传递,因此您可以修改在方法中收到的变量的值,而不会更改传递给函数的原始变量的值.

This is the same thing with scalars, they are passed by value, hence you can modify the value of the variable you received in your method and that won't change the value of the original variable that you passed to the function.

以下是简化理解的示例:

Here's an example to ease the understanding:

- (void)parentFunction {
    int i = 0;
    [self modifyValueOfPassedArgument:i];
    //i == 0 still!
}

- (void)modifyValueOfPassedArgument:(NSInteger)j {
    //j == 0! but j is a copied variable. It is _NOT_ i
    j = 23;
    //j now == 23, but this hasn't changed the value of i.
}

如果您希望能够修改i,则必须通过执行以下操作来传递 reference 的值:

If you wanted to be able to modify i, you would have to pass the value of the reference by doing the following:

- (void)parentFunction {
    int i = 0; //Stack allocated. Kept it that way for sake of simplicity
    [self modifyValueOfPassedReference:&i];
    //i == 23!
}

- (void)modifyValueOfPassedReference:(NSInteger *)j {
    //j == 0, and this points to i! We can modify i from here.
    *j = 23;
    //j now == 23, and i also == 23!
}

这篇关于在目标C中通过值或引用传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆