swift错误:无法转换类型为'UnsafeMutablePointer <UInt32>的值?到预期的参数类型'UnsafeMutablePointer <Int32>?”

时间:2019-08-30 15:36:52

标签: ios c swift pointers unsafemutablepointer

我正在研究 Xcode 10 iOS项目。 我正在尝试从Swift中调用C语言函数,该函数将指针作为参数。

c函数如下,

Compress.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

我试图将数组转换为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?””

我们非常感谢您的帮助。

谢谢。

让我知道是否需要澄清

0 个答案:

没有答案