如何在swift 4中使用UnsafeMutablePointer

时间:2017-09-20 18:44:12

标签: swift swift4 unsafemutablepointer

Objective-c有一个指向指针的概念。如果取消引用第一个指针,则可以访问原始

void makeFive(int *n) {
    *n = 5;
}

int n = 0;
makeFive(&n);
// n is now 5

当它被桥接到Swift 3时,它变成了UnsafeMutablePointer

func makeFive(_ n: UnsafeMutablePointer<Int>) {
    n.memory = 5
}
var n: Int = 0
makeFive(&n)
// n is now 5

但是,从Swift 4开始,此行为已更改,并且内存属性不再可用。

makeFive(_ :)函数的快速4等价是什么?

更新 感谢Hamish,我现在知道“记忆”被重新命名为指针。

1 个答案:

答案 0 :(得分:2)

请检查:https://developer.apple.com/documentation/swift/unsafemutablepointer

func makeFive(_ n: UnsafeMutablePointer<Int>) {
    n.initialize(to: 5)
}
var n: Int = 0
makeFive(&n)
// n is now 5