What's the Objective-C equivalent of Swift inout Parameters?

I am writing Objective-C code again and am trying to find the equivalent of Swift parameter notation inout

in Objective-C.

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

      

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID158

So far I found out: 1.inout is not objc compatible. 2.objc just doesn't have such a language feature.

However, when I design my API, I want to make it clear that this method changes its parameter. Is there a good way to do this in Objective-C? Thank!

+3


source to share


1 answer


To switch between obj-c and swift you can put your parameters in a class and pass this: fooobar.com/questions/413942 / ...

You can do it in Obj-C, you just don't see it very often.

- (void) doThing: (int *) varA andAnother:(int *) varB
{
    int temp = *varA;
    *varA = *varB;
    *varB = temp;
}

      



Then you call it with &

to pass the link

int i = 1;
int j = 2;
[self doThing:&i andAnother:&j]

      

+2


source







All Articles