简体   繁体   English

在 Swift 3 中用 UnsafeMutablePointer 编写

[英]Writing in UnsafeMutablePointer in Swift 3

The code below was working in previous versions of Swift, now compiler rejects it.下面的代码在以前版本的 Swift 中工作,现在编译器拒绝它。 I need this function to interop with Swift from ObjectiveC.我需要这个函数与 ObjectiveC 的 Swift 互操作。

@objc public static func myFunc(jdUT: Double, _ lon: Double, _ lat: Double,
                                           _ dayLen: Double, _ SbhDeg: Double, _ MgrbDeg: Double,
                                           omsk: UnsafeMutablePointer<Double>)
    {
        var z = somefuncion()
        // this line gives this error : Cannot assign to property: 'omsk' is a 'let' constant
        omsk.memory=z;
    }

The error message is misleading.错误消息具有误导性。 The memory property of Unsafe(Mutable)Pointer has been renamed to pointee in Swift 3: Unsafe(Mutable)Pointermemory属性已在 Swift 3 中重命名为pointee

let z = someFunction()
omsk.pointee = z
@objc public static func myFunc(jdUT: Double, _ lon: Double, _ lat: Double,
                                       _ dayLen: Double, _ SbhDeg: Double, _ MgrbDeg: Double,
                                       inout omsk: UnsafeMutablePointer<Double>)
{
    var z = somefuncion()
    // this line gives this error : Cannot assign to property: 'omsk' is a 'let' constant
    omsk.memory=z;
}

Adding inout before the omsk parameter should work.在 omsk 参数之前添加 inout 应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM