iOS开发-fj

OC项目中混编 swift

2019-01-11  本文已影响106人  HarlanHuang

一、环境:

cocoapods 1.6.0.bate.2
xcode 10.1

二、OC中混编 swift 步骤:

1、建立 oc 和 swift 桥接文件,如问题2中所示。

注:这个桥接文件是swift 文件中引用 oc 代码使用的,用的时候需要把 要使用的oc 的文件#import在里面。oc 使用 swift 文件不需要对这个桥接文件做操作

2、允许 target 使用 swift 代码,开启这个配置后 Swift 的标准库将能够在该项目使用(使用 oc和 swift 混编可能导致包体积变大十几兆)

Build Options 中
Embedded Content Contains Swift Code 改为 YES (Xcode 8.1 and earlier)
Always Embed Swift Standard Libraries 改为 YES (Xcode 8.2+)

3、支持使用@import导入文件,当需要导入 framework 的时候这个选项必须是YES

Defines Module 改为 YES

4、混编后哪些逻辑可以被另一种语言引用到?

swift中可以被 oc 使用的逻辑:

1、用public关键字

2、有bridging header的target中用internal关键字修饰;

3、使用@IBAction, @IBOutlet, 和 @objc标记的

oc中可以被 swift 中使用的逻辑:

和普通使用一样,头文件中定义的就可以使用

三、遇到的问题:

1、使用 pod 安装一个 swift 库,pod install 报错:

[!] Unable to determine Swift version for the following pods:
Alamofire does not specify a Swift version and none of the targets (YCMath-Online, YCMath345-iOS-test, Enterprise-Debug, DataTrack-Debug, and YCMath-Online-Backdoor) integrating it have the SWIFT_VERSION attribute set. Please contact the author or set the SWIFT_VERSION attribute in at least one of the targets that integrate this pod.

解决方案:

需要在每个 target 中设置swift 版本,搜索SWIFT_VERSION,如果没有,需要手动添加一个


1.png

2、安装成功后编译不通过,报错

__swift_FORCE_LOAD_$_swiftCoreFoundation", referenced from:
__swift_FORCE_LOAD_$_swiftCoreFoundation_$_NVDate in libNVDate.a(NVDate.o)
(maybe you meant: __swift_FORCE_LOAD_$_swiftCoreFoundation_$_NVDate)
xxx...之类的,很长很长的错误

解决方案:

在项目中手动建一个空的 swift 文件,这时候系统会自动弹出问你是否建立 swift 桥接文件,点击是。

2-1.png
2-2.png

建立好之后再次编译就可以成功了

3、安装Alamofire 网络请求库

 pod 'Alamofire', '~> 5.0.0.beta.1'

pod install 报错:

Specs satisfying the Alamofire (~> 5.0.0.beta.1) dependency were found, but they required a higher minimum deployment target.

解决办法:

platform :ios, '8.0' -> platform :ios, '10.0'

部分参考文档:https://www.jianshu.com/p/11d17575bbef

上一篇 下一篇

猜你喜欢

热点阅读