Access to extern const struct from Swift

1.5k views Asked by At

I can't access a c-struct from Swift, which was generated with Mogenerator.

The struct is in the implementation:

const struct MyAttributes MyAttributes = {
    .foo = @"foo",
};

And then in the header:

extern const struct MyAttributes {
    __unsafe_unretained NSString *foo;
} MyAttributes;

I added the header import to the bridging header. But I can't access the struct from Swift. With Objective C I can. I thought maybe Swift needs the struct declaration as it is in the implementation file, so I tried adding the .m file to the bridging header but this doesn't work. I think I can't change the structure of these files because they are generated by Mogenerator.

How do I fix this?

Thanks.

1

There are 1 answers

2
Martin R On BEST ANSWER

With

#import "YourClass.h"

in the bridging header file you can access the struct from Swift as

let fooAttr = MyAttributes.foo
println(fooAttr) // Output: "foo"