Returns char * in C and treats it as a String in swift
Now that I've created HelloFrameWork (Cocoa Touch Framework), I want to use the methods in it from swift.
hello.h should be a public header in Xcode
hello.h
#ifndef HELLO_H
#define HELLO_H
char* getHello();
#endif
hello.c
char* getHello() {
return strdup("Hello");
}
swift
let hello_char = getHello()
let hello: String = String(cString: hello_char)
//Memory release
free(hello_char)
Official Documentation Using Swift with cocoa and Object-C
Using strdup () doesn't seem to be beautiful either. To write better, you should give a destination argument on the swift side and put a value there.
Recommended Posts