本文介绍了快速将数据转换为UnsafeMutablePointer< Int8>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从swift中调用目标c类中的函数.

I'm calling a function in an objective c class from swift.

-(char *)decrypt:(char *)crypt el:(int)el{}

从swift调用此函数时,它会要求UnsafeMutablePointer<Int8>作为参数'crypt'

when calling this function from swift, it asks for an UnsafeMutablePointer<Int8> as the value for the parameter 'crypt'

'crypt'的值来自服务器,它是base64编码的字符串.因此,我对该字符串进行了解码,并得到了一个Data对象.

the value for the 'crypt' is comming from a server and it is a base64encoded string. So I decode that string and got a Data object.

let resultData = Data(base64Encoded: base64String)

现在我需要将此数据传递给上述函数.我试图将此Data对象转换为UnsafeMutablePointer<Int8>

Now I need to pass this data to the above mentioned function. I have tried to convert this Data object to a UnsafeMutablePointer<Int8>

resultData?.withUnsafeBytes { (u8Ptr: UnsafeMutablePointer<Int8>) in
                    let decBytes = tea?.decrypt(u8Ptr , el: el)}

但是它没有编译.给出以下错误

But it is not compiling. Gives below error

我对目标c不太了解.所以有人可以帮助我将此参数传递给目标c函数.

I don't know much about objective c. So could anyone help me to pass this parameter to objective c function.

推荐答案

您必须将UnsafeMutablePointer更改为UnsafePointer

UnsafePointer

resultData?.withUnsafeBytes {(bytes: UnsafePointer<CChar>)->Void in
            //Use `bytes` inside this closure

        }

UnsafeMutablePointer

 var data2 = Data(capacity: 1024)
data2.withUnsafeMutableBytes({ (bytes: UnsafeMutablePointer<UInt8>) -> Void in
                 //Use `bytes` inside this closure

            })

这篇关于快速将数据转换为UnsafeMutablePointer&lt; Int8&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 21:06