LoginSignup
2
1

More than 5 years have passed since last update.

char型バッファを引数にとるC関数をSwiftから呼ぶ

Last updated at Posted at 2017-01-11

たまにしか出会わないシチュエーションだからこそ書き残す。

Birdging-Headerを用意するのはObjective-CをSwiftから使う場合と同様。

コメントにて指摘頂きました。サンプルコードを書き直しました。(2017/1/12)

int getPath(char* buff, CFIndex maxPathSize);

こんなC言語の関数を呼び出す場合は以下のようにする。

var buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(MAXPATHLEN))
getPath(buff, CFIndex(MAXPATHLEN))
let str = String(cString: buff)
print(str)
...
buffer.deallocate(capacity: Int(MAXPATHLEN))

参考

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1