将Swift类添加到具有多个目标的Objective-C项目 [英] Adding Swift Class to Objective-C Project with Multiple Targets

查看:102
本文介绍了将Swift类添加到具有多个目标的Objective-C项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Obj-C项目,其中包含许多目标,这些目标均共享同一AppDelegate。我想桥接一个由选择目标使用的快速类。当我有一个目标时,我可以轻松地做到这一点。

I have an existing Obj-C project that houses many targets that all share the same AppDelegate. I want to bridge a swift class that is used by select targets. I can do this easily when I have one target.

当我向项目中添加快捷文件时,我选择了所需的目标并生成了必要的bridging-header.h文件,但是当我尝试导入这些文件时- swift.h文件,找不到它们。

When I add a swift file to the project, I select the desired targets and the necessary bridging-header.h files are generated, but when I try to import those -swift.h files, they are can't be found.

对于具有多个构建目标的项目,我是否缺少步骤?

Are there steps I'm missing when it comes to projects that have multiple build targets?

编辑-更多详细信息

我想为我的项目添加更多细节

I wanted to add a little bit more detail on how my project is set up.

我有一个框架,我们将其称为用Obj-C编码的AppFactory。我有多个构建目标,这些目标根据目标plist中的信息生成不同版本的Core应用。我希望这些应用程序可以利用快速文件。在我的构建设置中,Defines Module标记为Yes,我已经创建了这个swift类:

I have a Framework, we'll call it AppFactory, coded in Obj-C. I have multiple build targets that generate different versions of the Core app depending on information in that target's plist. I want a swift file to be utilized by these apps. In my build settings the Defines Module is marked to Yes, I have create this swift class:

@objec class SwiftClass: NSObject {

}

这样做,Xcode生成了正确的Briding-Header .h文件。

And in doing that, Xcode generated the proper Briding-Header.h files.

根据 Apple指南当您有多个构建目标时,导入标头应包含ProductName / ProductModuleName-Swift.h,应该由Xcode自动生成。

According to Apple Guides when you have multiple build targets your import header should include the ProductName/ProductModuleName-Swift.h, which should be auto generated by Xcode.

当我查看派生数据文件夹时,ProductModuleName-Swift.h确实存在,在每个目标build文件夹下, AppFactoryCore文件夹。

When I look in to my derived data folder, the ProductModuleName-Swift.h does exist, with in each targets build folder, under the AppFactoryCore folder.

推荐答案

我找到了解决问题的方法。

I found a solution to my problem.

Swift文件目标:
而不是拥有SwiftClass.sw ift目标同时针对框架和所选目标(AppA,AppB和AppC),我回退了广告,只针对框架AppFactoryCore。

Swift File Targets: Instead of having SwiftClass.swift target both the framework and the selected targets (AppA, AppB & AppC), I backpedaled and solely targeted the framework, AppFactoryCore.

构建设置(打包>定义模块):
我还原了每个应用目标的 Defines Module 属性从 YES 变为 NO ,并将此属性设置为 YES 作为框架目标。

Build Settings (Packaging > Defines Module): I reverted each app target's Defines Module property from YES to NO, and set this property to YES for the framework target.

快速类声明:
指南指出:


因为为框架目标生成的标头是框架公众的一部分界面,只有标有public或open修饰符的声明才会出现在框架目标的生成的标头中。

Because the generated header for a framework target is part of the framework’s public interface, only declarations marked with the public or open modifier appear in the generated header for a framework target.

所以我添加了访问控制修饰符到我的班级和班级职能

So I added access control modifiers to my class and class functions

@objc open class SwiftClass: NSObject {
    //Code
}

导入标题
由于SwiftClass.swift仅针对框架目标,并且实际上它是一个正在使用的框架,因此标题将SwiftClass.swift导入通用AppDelegate就是

Import Header: Since SwiftClass.swift is only targeting the framework target, and it is in fact a framework that is being used, the header import SwiftClass.swift into the universal AppDelegate was

#import <AppFactoryCore/AppFactoryCore-Swift.h>

完成所有先前所述的修改后,这才变得可见。

Which finally became visible once all previously stated modifications were done.

现在该文件是所有目标的全局文件,我添加了一个自定义属性来标识正在运行的目标是否是应该利用SwiftClass.swift的目标。

Now that the file is global to all targets I added a custom attribute to identify if the target running was is one that should utilize SwiftClass.swift.

希望这可以帮助其他尝试完成相对类似任务的人!

Hope this helps anyone else trying to accomplish a relatively similar task!

这篇关于将Swift类添加到具有多个目标的Objective-C项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆