简体   繁体   English

如何在Objective-C中使用Swift文件?

[英]How to work with Swift file in Objective-C?

I want to use one third party library which is written in Swift but I am working in Objective-C environment. 我想使用一个用Swift编写的第三方库,但是我在Objective-C环境中工作。 How to do that? 怎么做? Until now what I have done is: 到目前为止,我所做的是:

  1. Drag and drop the third party library into my project. 将第三方库拖放到我的项目中。
  2. It is asking for the bridging header I clicked on yes. 它要求我单击“是”的桥接头。
  3. In Build settings defines Modules no to yes in both. 在“构建设置”中, defines Modules否” defines Modules为“ yes
  4. Product Module Name showing like $(PRODUCT_NAME:c99extidentifier) . 产品模块名称显示$(PRODUCT_NAME:c99extidentifier) I have tried with my project name also. 我也尝试过使用我的项目名称。

Now I am getting plenty of errors like below: 现在,我收到很多错误,如下所示:

pDatePicker.swift:50:9: Consecutive declarations on a line must be separated by ';' pDatePicker.swift:50:9:行上的连续声明必须以';'分隔

and

Ambiguous use of 'open'. 歧义使用“开放”。

I am getting errors around 50 to 60 same as above errors. 我收到与上述错误相同的大约50到60的错误。 Where is my mistake? 我的错误在哪里? Can anybody help me? 有谁能够帮助我?

Make sure, that your swift version in settings is the same as this lib uses; 确保设置中的快速版本与此lib使用的版本相同; if not, try to translate it by yourself, or set Use Legacy Swift Version to YES 如果不是,请尝试自行翻译,或将“使用旧版Swift版本”设置为“是”

Are you able to see projectName-Swift.h file in your project ? 您可以在项目中看到projectName-Swift.h文件吗?

Demo Example is available on github. 演示示例可在github上找到。

https://github.com/hasyapanchasara/UsingSwiftInObjectiveC https://github.com/hasyapanchasara/UsingSwiftInObjectiveC

If yes then, Hope you have imported #import "projectName-Swift.h" in Objectice C .m file ? 如果是,那么,希望您已在Objectice C .m文件中导入#import“ projectName-Swift.h”吗?

#import "projectName-Swift.h"

Then only you would be able to access objective c files in swift code. 这样,只有您才能以快速代码访问目标c文件。

Refer 参考

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

  • Check in build setting 签入构建设置
  • Objective-C Bridging Header Objective-C桥接标头
  • ProjectName-Bridging-Header.h has perfect location. ProjectName-Bridging-Header.h具有完美的位置。

if there is problem with that path than bridging will not work properly , that why when you compile #import "projectName-Swift.h" is not getting generate. 如果该路径存在问题,则桥接将无法正常工作,这就是为什么在编译#import "projectName-Swift.h"未生成的原因。

Download 下载

Download above sample project. 下载以上示例项目。

在此处输入图片说明

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

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