You cannot pass an argument by reference in (Objective-) C. What you probably mean is to pass the address of the variable as an argument to the method so that the method can set the value of the variable using a pointer.
However, this does not work with properties. self.obj1 is just a convenient entry for calling the [self obj1] method, which returns the value of the property. And it is impossible to accept the address of the return value.
What you can do is
Pass the address of the corresponding instance variables:
[SomeClass someFuncWithParam1:&_obj1 param2:&_obj2]
The disadvantage is that property access methods are excluded. This may or may not be in your case.
Pass the address of temporary local variables:
NSObject *tmpObj1; NSObject *tmpObj2; [SomeClass someFuncWithParam1:&tmpObj1 param2:&tmpObj2]; self.obj1 = tmpObj1; self.obj2 = tmpObj2;
Both solutions are not very pleasant. Alternatively, you can pass the entire object ( self ) to a helper method, which then initializes both properties.
Or you define a custom class with only the properties obj1 and obj2 , and make the helper method return an instance of that custom class.
Martin r
source share