从Swift访问extern const struct [英] Access to extern const struct from Swift
问题描述
我无法从Mogenerator生成的Swift中访问c结构.
I can't access a c-struct from Swift, which was generated with Mogenerator.
该结构体在实现中:
const struct MyAttributes MyAttributes = {
.foo = @"foo",
};
然后在标题中:
extern const struct MyAttributes {
__unsafe_unretained NSString *foo;
} MyAttributes;
我将头文件导入添加到了桥接头文件中.但是我无法从Swift访问结构.使用目标C,我可以.我以为Swift可能需要struct声明,就像它在实现文件中一样,所以我尝试将.m文件添加到桥接头中,但这不起作用.我认为我无法更改这些文件的结构,因为它们是由Mogenerator生成的.
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.
我该如何解决?
谢谢.
推荐答案
使用
#import "YourClass.h"
在桥接头文件中,您可以使用以下方式从Swift访问结构:
in the bridging header file you can access the struct from Swift as
let fooAttr = MyAttributes.foo
println(fooAttr) // Output: "foo"
这篇关于从Swift访问extern const struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!