Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift convert Data to UnsafeMutablePointer<Int8>

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

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

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

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)

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

'UnsafeMutablePointer' is not convertible to 'UnsafePointer<_>'

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

like image 907
udi Avatar asked Dec 17 '22 23:12

udi


1 Answers

you have to change UnsafeMutablePointer to 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

            })
like image 191
Abdelahad Darwish Avatar answered Jan 01 '23 00:01

Abdelahad Darwish