Trying to use a struct from ObjC to Swift doesn't seem to be that easy. I end up getting a Unsafe pointer that I don't know if I can cast reliably.
Here is the code:
//
// In OBJC land
//
// Type declared as a struct
typedef struct node {
int children_count;
} node_t;
// Super class has a property
@property (nonatomic, assign, readonly) node_t *node;
//
// In SWIFT land
//
// Derived class tries to set the property inside the C struct
let n: UnsafeMutablePointer<node_t> = super.node // As swift compiler sees it
n.children_count = 0 // ERR!!!
Do I really need to apply unsafeBitcast here or is there a simpler/safer and more elegant way to convert what seems to be a frequent scenario?
UPDATE:
I tried using memory to access the elements of the struct and I am getting a EXC_BAD_INSTRUCTION
var node: node_t = self.node.memory
node.children_count = 42
UPDATE CONT'D & FINALE
I got it to work. Thanks to @matt's patience for making sure I groked 'memory' access completely. The other trick is to realize the assignment in one continuous statement like this:
var node: UnsafeMutablePointer<node_t> = self.node
node.memory.children_count = 42
If I do the following, the change doesn't get committed passed the function call:
var node: node_t = self.node.memory
node.children_count = 42
nodecauses that struct to be copied.