Argument passing regarding ivars
I am passing an iVar into a function. The iVar is a double. The value of the iVar inside the function is correct, but the value of the iVar outside the function is not changed. This must have something to do with the way that I am receiving the iVar into the function. How do I pass a double iVar in so that its value is changed in the object and not only in the function? I do the same thing with pointers and the results are as expected, so I think I am not understanding scalar argument passing in c/objective-c.
The code looks like this: .h
@interface myClass: NSObject
{
double myDouble;
}
- (void)func1;
- (void)func2: (double)myDouble;
.m
- (vo开发者_开发知识库id)func1 {
myDouble = 1.2
NSLog(@"Before func2 myDouble = %f", myDouble);
[self func2: myDouble];
NSLog(@"After func2 myDouble = %f", myDouble);
}
- (void)func2: (double)adouble
{
NSLog(@"In func2 before operation adouble = %f",adouble);
NSLog(@"In func2 before operation myDouble = %f", myDouble);
adouble = 3.4;
NSLog(@"In func2 after operation adouble = %f",adouble);
NSLog(@"In func2 after operation myDouble = %f", myDouble);
}
results in:
Before func2 myDouble = 1.2
In func2 before operation adouble = 1.2
In func2 before operation myDouble = 1.2
In func2 after operation adouble = 3.4
In func2 after operation myDouble = 1.2
After func2 myDouble = 1.2
I am assuming your function probably looks something like this right now:
void doSomethingWithDouble(double number) {
number = number *2;
}
If you want to have the function directly modify the double, then you would change it to this:
void doSomethingWithDouble(double *number) {
*number = *number / 2.0;
}
This modifies the passed value, but you have to pass a pointer. An example of this would be:
double test = 2.0;
doSomethingWithDouble(&test);
For the record though, this is all just plain C, not Objective-C.
@Stefan isn't quite correct; a double gets passed-by-value not because it is not a pointer type but because everything is passed-by-value in C and Objective-C. You can think of parameter passing as initializing a local variable. Using your original myClass and func2 and the declarations:
myClass anInst = [myClass new];
double someNum = 4.2;
then the call:
[anInst func2:someNum];
is evaluated as:
[myClass func2:someNum];
=> enter a new method(function) scope, creating the local vars of func2
adouble = someNum; // that is func2's adouble and the callers someNum
// only the value in someNum is copied into adouble
adouble = 3.4; // the body of func2 - this has no effect on someNum
<= return from func2's scope
// someNum has not changed
The same thing would happen if the parameter had pointer type, the only difference is that though you pass the pointer by value you can modify the contents of the object/variable/memory the pointer value is referring to - but you still don't change the pointer.
C++ (and Objective-C++) do support pass-by-reference, but that is another topic...
Double is not a pointer type and therefore gets passed by value. You would need to use a pointer type like NSNumber in order for the change to be affected in the calling function.
EDIT:
But as @Tom Dalling pointed out, NSNumber is immutable. so you might have to have your function return the value that you need instead.
精彩评论