Swift到Objective-C标头不包含Swift类 [英] Swift to Objective-C header does not contain Swift classes
问题描述
我正在尝试在我的Objective-C代码中使用Swift类,但是我的Swift类似乎没有出现在生成的标头中.结果,我的构建失败,并显示使用未声明的标识符'HelloWorld'".
I'm attempting to use Swift classes in my Objective-C code, however my Swift classes don't seem to appear in the generated header. As a result, my build fails with "Use of undeclared identifier 'HelloWorld'".
我使用模板创建了一个名为TestApp的项目.
I used the templates to create a project called TestApp.
我的目标中具有以下构建设置:
I have the following Build Settings in my target:
- 产品名称:TestApp
- 产品模块名称:TestAppModule
- 定义模块:是
Apple的文档说要使用#import <TestApp/TestAppModule-Swift.h>
,但这是行不通的.
Apple's documentation says to use #import <TestApp/TestAppModule-Swift.h>
but this doesn't work.
相反,我在我的".m"文件中使用了#import "TestAppModule-Swift.h"
.似乎找到了.
Instead, I'm using #import "TestAppModule-Swift.h"
in my ".m" file. It seems to find this.
我能够导航到它,看起来像这样...
I'm able to navigate to it, and it looks like this...
// Generated by Swift version 1.0 (swift-600.0.34.4.5)
#if defined(__has_include) && __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif
...etc...
但其中未定义任何类.
我在项目中有一个Swift文件,看起来像这样...
I have a Swift file in the project that looks like this...
class HelloWorld {
func hello() {
println("hello world")
}
}
为什么使用标准头文件位置#import <TestApp/TestAppModule-Swift.h>
不能正常工作?
Why isn't this working using the standard header file location #import <TestApp/TestAppModule-Swift.h>
?
如何在该头文件中获取快捷类,因此不会出现未声明的标识符"错误?
How can I get my swift classes in that header file, so I won't get the "undeclared identifier" error?
推荐答案
这就是我如何使其工作的方式.您可以在此处找到更大规模的答案.
Here's how I have gotten it to work. You can see a more large-scale answer here.
更改此:
class HelloWorld {
func hello() {
println("hello world")
}
}
收件人:
@objc class HelloWorld {
class func newInstance() -> HelloWorld {
return HelloWorld()
}
func hello() {
println("hello world")
}
}
然后,在您的ObjC文件中:
Then, In your ObjC file:
#import "TestApp-Swift.h"
然后这样呼叫:
HelloWorld * helloWorld = [HelloWorld newInstance];
[helloWorld hello];
这篇关于Swift到Objective-C标头不包含Swift类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!