CocoaPods with Swift

前言

最近,经小组讨论,打算使用Swift3.0开发下一个新项目,碍于很多第三方类库都是OC的类库,所以类库方面只能慢慢从OC类库过渡到Swift类库。
下面会简述pods中导入Swift库的过程,Let’s go!

更新pods

以ObjectMapper第三方开源类库为例
ObjectMapper是一个基于Swfit语言实现JSON和Model间互转的类库
传送门:https://github.com/Hearst-DD/ObjectMapper

ObjectMapper 官方说明,类库最新版本是pod 'ObjectMapper', '~> 2.2'
CocoaPods版本需要 0.36 or later

然后我打开终端搜索ObjectMapper,输入pod search objectmapper
结果显示最新的ObjectMapper版本号是1.3.0
(LOL显然不对,官方说最新的2.2,但是对CocoaPods版本需求是0.36 or later)

应该是CocoaPods版本问题,升级CocoaPods!
终端输入:$ sudo gem update cocoapods 

pod install

终端输入:
1、cd 你的项目profile文件路径
2、pod install
到这里,发现返回

Analyzing dependencies
[!] The dependency `ObjectMapper (~> 2.2.0)` is not used in any concrete target.

[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

网上资料说,Podfile不要使用TextEdit(文本形式编辑),会出现问题,请尽量使用xcode或者终端编辑。

  • 可能是语法问题?

解决方法:
还是使用TextEdit打开Podfile,语法更改如下

platform :ios,’8.0’

#兼容swift库
use_frameworks!

#导入所需库
target 'SmartHome' do
pod 'AFNetworking'
pod 'ObjectMapper', '~> 2.2.0'
end

#适配当前xcode8.1 swift3.0版本,更新库指定swift3.0
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
        end
    end
end

ok,pod install !

终端输入: pod install

其中可能遇到会遇到的问题:

1、pods兼容swift库

[!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The Swift Pods being used are: Alamofire and SwiftyJSON

去stackoverflow搜了发现要在Podfile加上

use_frameworks!

2、swift类库版本问题

更新swift类库后,运行xcode报错如下

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

原因:swift类库的版本,与当前xcode的swift版本不一致
解决方法:

#适配当前xcode8.1 swift3.0版本,更新库指定swift3.0
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
        end
    end
end

ha

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,191评论 4 61
  • 项目组件化、平台化是技术公司的共同目标,越来越多的技术公司推崇使用pod管理第三方库以及私有组件,一方面使项目架构...
    swu_luo阅读 22,518评论 0 39
  • CocoaPods 是什么? CocoaPods 是一个负责管理 iOS 项目中第三方开源库的工具。CocoaPo...
    朝洋阅读 25,815评论 3 50
  • 骤雨疏梧桐,窗外栏杆谁凭望。迷途不知何处,苍苍。离魂孤魄归何方? 咽声随处荡,夜半时分冷卧床。泪又落了几行,数行。...
    狄枫Leslie阅读 3,199评论 0 5
  • 日计划训练营线下的小伙伴们进行了日计划总结分享会。 从线下活动的输出,我汲取到了养分。 加一:制定每一周计划的计划...
    舞倾城夏阅读 1,001评论 0 0