For performance reasons I need drop into C for my inner loop. I'm trying to debug it and want to be able to store a string in a C struct.
typedef struct Slot {
char name[12];
Byte fixed;
Byte loaded;
Obj obj;
} Slot;
At the moment this is just for debugging purposes, so I'm not too particular about how to make this happen (i.e., char* name? char name[12]? something else?)
On the swift side this is literally the best I've been able to come up with so far:
var vars = [String]()
...
var i = 0
for v in vars {
let array: [UInt8] = Array(v.utf8)
memory.pointee.slots[i].name.0 = Int8(array[0])
memory.pointee.slots[i].name.1 = Int8(array[1])
memory.pointee.slots[i].name.2 = Int8(array[2])
memory.pointee.slots[i].name.3 = Int8(array[3])
memory.pointee.slots[i].name.4 = Int8(array[4])
memory.pointee.slots[i].name.5 = Int8(array[5])
memory.pointee.slots[i].name.6 = Int8(array[6])
memory.pointee.slots[i].name.7 = Int8(array[7])
memory.pointee.slots[i].name.8 = Int8(array[8])
memory.pointee.slots[i].name.9 = Int8(array[9])
memory.pointee.slots[i].name.10 = Int8(array[10])
memory.pointee.slots[i].name.11 = Int8(array[11])
}
which doesn't work if the String is less than 12 characters. (And is ridiculous anyway)
How is one supposed to do this?
Edit: Not duplicate of Obtain a pointer to a C char array in Swift because they are trying to go from C to Swift not Swift to C and because I'm not necessarily restricted by a fix length char array, that was merely the "best" solution I've found so far.
Edit 2: This IS a duplicate of Convert an String to an array of int8. Which solved my problem (with the recent update). Thanks.