I'm aware of this answer, but this is not the same thing - thats passing a pointer to be initialised with an allocation.
I'm interfacing with a C library that has the following structure definition:
typedef struct myStruct { unsigned char var [50]; } myStruct;
There is a function to which you pass the address of the structure - normally stack based not heap, thus:
myStruct mine;
initMyStruct(&mine);
This initialises the content of the struct - the caller does not know the internal format of the memory, hence the obfuscation.
In Swift, I'm creating a class which will encapsulate the structure and interface with other C functions which operates on it.
class MyClass {
var mine : myStruct
init() {
initMyStruct(&mine)
// Error: Variable 'self.msg' passed by reference before being initialized
}
}
I can't for the life of me work out how to initialise the structure, or use a point instead if that is an alternative.
mine = myStruct()
// Fails because you aren't providing the value of the member 'var'
mine = myStruct((CUnsignedChar(), CUnsignedChar(), /*... repeat to 50 */))
// Cannot find an overload for 'init' that accepts the arguments
Please note, this is passing the address of an already allocated (stack based) structure to a function that has been imported as
CInt initMyStruct(str: CMutablePointer<myStruct>)
It is NOT passing a pointer to be allocated by the C library in question which seems to be a more common requirement and is answered elsewhere.
Swift currently doesn't support fixed size arrays either.
I can't see how to satisfy the initialisation of the structure or make Swift think that it is going to be done so by the call.
Any help greatly appreciated!
See Question&Answers more detail:os