0

我正在开发Xcode 10 iOS 项目。我试图从 Swift 中调用一个 C 语言函数,它以指针为参数。

c函数如下,

压缩.h文件

short* Decompress(int *dataIn, int channelNum,int sample_cnt,long packet_cnt) ;

我正在尝试从MyClassfile.swift调用此方法

来自 xcode 的预期输入如下

  var dataOut = Decompress(dataIn: UnsafeMutablePointer<Int32>!, channelNum: Int32, sample_cnt: Int32, packet_cnt: Int)

因为第一个参数的输入是可选类​​型,如“ UnsafeMutablePointer< Int32>!

我试图将数组转换为 UnsafeMutablePointer 类型

代码如下,

var uint32PktArray = [UInt32]()

for index in stride(from: 0, to: somebuffer.count, by: 4){
          //doing some functionality here
          uint32PktArray.append(value)
}

          let uint32Pointer : UnsafeMutablePointer<UInt32>? = UnsafeMutablePointer<UInt32>.allocate(capacity: uint32PktArray.count)
            uint32Pointer!.initialize(from: &uint32PktArray, count: uint32PktArray.count)

          print(type(of: uint32Pointer))// returns UnsafeMutablePointer<UInt32>?

但输入参数需要一个可选类型,方法调用如下

 var dataOut = Decompress(uint32Pointer,0, Int32(compHeaderECG0.smpl_cnt), Int(compHeaderECG0.pkt_cnt))
                print(dataOut)

编译时我收到上述代码的错误,“无法转换'UnsafeMutablePointer'类型的值?' 到预期的参数类型'UnsafeMutablePointer?'”

非常感谢任何帮助。

提前致谢。

让我知道是否需要任何澄清

4

0 回答 0