ㄷㅣㅆㅣ's Amusement
[Swift] How to convert from void* to char* 본문
[Swift] How to convert from void* to char*
반응형
How to convert from void* to char*
I had to use f**king idiot library which is written by C++ in Swift project.
That library use void* for call back function to return.
(There are so many call back event, but that library use only one function pointer (returning void*) like above.)
1 2 | typedef void (* ANY_CB)(void *); void registerListener(ANY_CB *cbs, ANY_CB cb, ANY_EVENT event); | cs |
And I faced to problem.
The library call back with some log. so I had to convert void* to char*, then convert to String. because We need to check the library logs on Xcode console.
The solution is very simple. but I could find it nowhere..
(I try to convert UnsafeMutableRawPointer to UnsafeMutablePointer<CChar>, but I failed)
so I post this blog.
1 2 3 4 5 6 7 | registerListener(listener, { (log) in guard let log = log else { return } let opaquePtr = OpaquePointer(log) let logPtr = UnsafeMutablePointer<CChar>(opaquePtr) AISTTSManager.shared.onInfo(log: String(cString: logPtr)) }, ON_EVENT) | cs |
If you use OpaquePointer, then you can convert it to any other pointer. That's it!!
반응형
'Programming > iOS' 카테고리의 다른 글
[swift] getter/setter, subscript, property wrapper에서 get/set의 상이한 동작 (1) | 2020.06.18 |
---|---|
WWDC 2018 (0) | 2018.08.21 |
How to use private git repository under carthage. on Mac OS (0) | 2017.08.22 |
[iOS/Objective-C] Audio Player tutorial using AVPlayer (AVFoundation) -- 1 (0) | 2016.02.24 |
[iOS/Objective-C] Video Player tutorial using AVPlayer (AVFoundation) (2) | 2016.02.23 |
Comments