简体   繁体   English

在 Obj C 模块中导入一个 Swift 模块

[英]Import a Swift module in Obj C module

I have a xcworkspace with two xcodeprojects inside (one is a static library where all the base functionalities are implemented and the other is a demo project which uses classes from first one) - and all the code so far has been written in Objective C.我有一个 xcworkspace,里面有两个 xcodeprojects(一个是 static 库,其中实现了所有基本功能,另一个是使用第一个类的演示项目) - 到目前为止,所有代码都是用 Objective C 编写的。 What I want to achieve is create a Swift class in the static library and then access it from an Objective C class in the 'demo' xcodeproj. What I want to achieve is create a Swift class in the static library and then access it from an Objective C class in the 'demo' xcodeproj.

I have created this 'Test.swift' class and a bridging header that was created automatically (changed the Defines Module property to YES in the Build setting), and everything works well - I can access it from Obj C classes in the same project.我已经创建了这个“Test.swift”class 和一个自动创建的桥接 header(在 Build 设置中将 Defines Module 属性更改为 YES),并且一切正常 - 我可以从同一项目中的 Obj Z0D61ZB0370CAD1D3E412D7 类中访问它。 Next, I am creating a new "DemoTest.swift" Swift class in the 'demo' project and subclassing the Test.swift (which works).接下来,我将在“演示”项目中创建一个新的“DemoTest.swift”Swift class 并将 Test.swift 子类化(有效)。 However, when trying to access this class from an Objective C file in the 'demo' project, the compiler doesn't recognize my "base" module -但是,当尝试从“演示”项目中的目标 C 文件访问此 class 时,编译器无法识别我的“基础”模块 -

In file included from 
xxxxx/AppDelegate.m:26:
xxxxx/mpdemo-Swift.h:189:9: fatal error: module 'mpbba' not found
@import mpbba;
 ~~~~~~~^~~~~
1 error generated.

My question is how can I have a Swift code imported into another Objective C module?我的问题是如何将 Swift 代码导入另一个目标 C 模块?

Here's what to do:这是做什么:

  1. configure an Objective-C bridging header配置 Objective-C桥接 header
  2. Click on your Xcode Project file单击您的 Xcode 项目文件
  3. Click on Build Settings点击构建设置
  4. Find the Search bar and search for Defines Module .找到搜索栏并搜索定义模块
  5. Change the value to Yes .将值更改为Yes
  6. Search Product Module Name.搜索产品模块名称。
  7. Change the value to the name of your project.将值更改为项目的名称。
  8. In class, add the following: #import "YourProjectName-Swift.h"在 class 中,添加以下内容: #import "YourProjectName-Swift.h"

Hope this will work.希望这会奏效。 let me know if you need anything else.需要帮助请叫我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM