什么是Objective-C相当于Swift的inout参数?

时间:2017-07-05 08:03:29

标签: objective-c swift

我重新编写Objective-C代码,并试图在Objective-C中找到Swift的inout参数表示法。

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

到目前为止,我已经了解到:1.inout不是objc兼容的。 2.objc根本没有像这样的语言功能。

然而,当我设计我的API时,我希望它清楚这个方法正在修改它的参数。有没有什么好方法可以在Objective-C中实现这一目标?谢谢!

1 个答案:

答案 0 :(得分:5)

对于obj-c和swift之间的桥接,你可以把你的参数放在一个类中并传递给它:https://stackoverflow.com/a/26288083/78496

你可以在Obj-C中进行,你只是不经常看到它。

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

然后,您使用&调用它来传递参考

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