Swift - how to use malloc?

I am trying to translate an Objective-C application to Swift and I have no idea how to implement malloc.

Can I use it in Swift?

thank

+3


source to share


1 answer


You need to implement the bridge header when you are using ObjC or C from Swift. The functions exported by your bridge header are then available in your Swift app / module. See here for an overview.

If you just need to "name some code" on the C side, then the functions exported from C are basically Swift wrappers. However, if you need to interact with the data returned from these functions, especially if malloc'd and not a simple primitive - Swift has a number of C related types ready for you to use (see here for specifics).



Also, if you are trying to wrap or interact with C ++ code, you cannot directly do it from Swift. You have to set up an initial interface with ObjC or C for C ++ code and then move it to Swift. It's not really much fun, but luckily it's not used as often as an ObjC (primarily) or C bridge.

... and for what it's worth, if you don't need low-level Core Audio for whatever reason (provided for example porting an app you already have), AVAudioEngine (iOS8 +) , so much easier for any applicable use case. than Core Audio, and is readily available in Swift.

+1


source







All Articles