21
18

More than 5 years have passed since last update.

Swiftでポインタが指している値にアクセス

Posted at

Cで書かれたライブラリを使う時に調べたのでメモ。
Using Swift with Cocoa and Objective-C を参考にしました。

UnsafePointer<T>.memory

ポインタが指している値にアクセスするには memory を使うようです。

構造体のポインタからメンバー変数にアクセスする例

hoge.h
typedef struct tagHoge {
    int foo;
    int baa;
} Hoge;

Hoge *GetHoge();
test.swift
var a = GetHoge()
//a.foo = 10      // エラー
a.memory.foo = 10 // OK
println(a.memory.foo)

Swiftから見ると、GetHoge関数の戻り値の型はUnsafePointer<Hoge>となるので
これで良いようです。

21
18
0

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
21
18