Reputation: 4320
I've imported a C struct into my Swift project, however I'm unable to get or set the init
property on the imported struct, getting the following error:
error: 'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type
The original C struct is something like:
struct test_t {
int init;
};
and my Swift code that fails looks like:
var t = test_t()
let i = t.init // error thrown here
t.init = 10 // error thrown here
I can understand that the name is an issue, so is there some other way to set properties on structs in Swift, perhaps involving some meta programming or dynamic dispatch?
Upvotes: 0
Views: 82
Reputation: 54706
You can declare variable names to use reserved keywords by using the backtick around their names like
struct Test {
var `init`:Int
}
You can do the same with the call to an imported property/method whose name is a reserved keyword when you access/call that property/method.
var t = test_t()
let i = t.`init`
t.`init` = 10
Upvotes: 1