【问题标题】:Access to extern const struct from Swift从 Swift 访问 extern const 结构
【发布时间】:2014-12-08 19:11:08
【问题描述】:

我无法从 Swift 访问使用 Mogenerator 生成的 c-struct。

结构在实现中:

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

然后在标题中:

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

我将标题导入添加到桥接头。但我无法从 Swift 访问结构。使用 Objective C 我可以。我想也许 Swift 需要结构声明,因为它在实现文件中,所以我尝试将 .m 文件添加到桥接头,但这不起作用。我认为我无法更改这些文件的结构,因为它们是由 Mogenerator 生成的。

我该如何解决这个问题?

谢谢。

【问题讨论】:

  • 或者在 mogenerater 中使用 swift 标志。那么就不需要任何桥接了。
  • 我的大部分代码仍在 Objective C 中。

标签: ios swift mogenerator


【解决方案1】:

#import "YourClass.h"

在桥接头文件中,您可以从 Swift 访问结构体

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

【讨论】:

  • 嗯,这是我尝试的第一件事,但由于某种原因它不起作用。不过,我的代码更复杂,所以原因一定在其他地方。刚刚在一个新项目中进行了测试,它可以工作。
  • 我遇到了同样的事情。我最初虽然由于 xcode 中缺少代码完成而无法正常工作。任何人都知道是否有办法让快速代码完成适用于这样的结构?
猜你喜欢
  • 2014-09-11
  • 2011-11-09
  • 2020-03-15
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多