Access to extern const struct from Swift

℡╲_俬逩灬. 提交于 2019-12-12 05:29:32

问题


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:


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"


来源:https://stackoverflow.com/questions/27365150/access-to-extern-const-struct-from-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!