我正在try 一个混合了Swift和ObjectiveC的项目,但运气不佳.
我的项目由6个目标组成:
- 应用程序
- 果心
- 果心Test
- 总结
- WatchKit扩展
- WatchKit 应用程序
我已经能够添加一个Swift类,该类有一个目标成员身份(应用程序),并使用从应用程序-Bridging-Header.h个应用程序传递的Objective代码
取而代之的是,我正在与"快速转换为Objective代码"的范例作斗争.
到目前为止,我一直在做的是:
- Add a new Swift file to the 果心 project specifying the four memberships (应用程序, 果心, 总结, WatchKit Ext)
- XCode asks to create -Bridging-Header.h for three projects 果心, 总结 and WatchKit Ext (应用程序-Bridging-Header.h already exists since it was used previously), and I consent (don't know why it creates those files inside the same group folder where I add my Swift class but nevermind)
- 我创建了Swift类,在class之前添加@objc键
- 我go 判断是否所有模块都设置了Objective-C Generated Interface Header Name,是的
- 我go 判断是否所有模块中的Defines Module都设置为No,主项目中的设置为Yes,是的
- 我写下所有四个目标的-Swift.h个文件的导入
- 在我的Objective-C类中,我try 使用我的新Swift类,显然它似乎有效(甚至自动完成)
- 我试图构建该项目,但失败的原因是找不到-Swift.h文件
我已经遵循了Mix and Match guide of 应用程序le多个线程,包括this one,似乎解决了我的同一个问题,但它不起作用.
他们还try 删除派生数据,并多次清理整个项目,但都没有成功.
EDIT 1 : More details
这是我的Swift课程的宣言
@objc class MyClass: Object {}
这是Objective中Swift类的用法片段
MyClass *app = [[MyClass alloc] init];
如果我避免使用#导入-敏捷的h构建失败,说我正在使用未声明的idenfier"MyClass",这是正确的.
如果我使用#导入-敏捷的h文件表示模块X找不到模块Y等等.
EDIT 2 : Swift module name
通过查看每个目标的SWIFT_OBJC_INTERFACE_HEADER_NAME属性,我看到它是使用这个语法$(SWIFT_MODULE_NAME)-Swift.h构建的,我需要将SWIFT_MODULE_NAME更改为MODULE_NAME吗?
EDIT 3 : Imports
这是中Swift标题的导入集.m ObjectiveC文件.
#import "ProjectName_应用程序-Swift.h"
#import "ProjectName_果心-Swift.h"
#import "ProjectName_总结-Swift.h"
#import "ProjectName_WatchKit_Extension-Swift.h"
EDIT 4 : Cross visibility of -Swift.h files
我try 了以下几点:
- Added a Swift class only in target 应用程序
- XCode创建了桥接头
- Inserted #import "ProjectName_应用程序-Swift.h" in ObjectiveC file used only by 应用程序
- 在这个文件中使用了Swift类
- 它编译!而且我能使用Swift类
When the Swift class is defined for multiple targets and ObjectiveC file is used by those multiple targets it doesn't work. The build errors are like this: Target X -> "ProjectName_TargetY-Swift.h" file not found. Seems like a target cannot see other targets -Swift.h files.
我做错了什么?