前言
最近,經(jīng)小組討論给僵,打算使用Swift3.0開發(fā)下一個新項目凡人,礙于很多第三方類庫都是OC的類庫,所以類庫方面只能慢慢從OC類庫過渡到Swift類庫坪哄。
下面會簡述pods中導(dǎo)入Swift庫的過程质蕉,Let’s go!
更新pods
以O(shè)bjectMapper第三方開源類庫為例
ObjectMapper是一個基于Swfit語言實現(xiàn)JSON和Model間互轉(zhuǎn)的類庫
傳送門:https://github.com/Hearst-DD/ObjectMapper
ObjectMapper 官方說明翩肌,類庫最新版本是pod 'ObjectMapper', '~> 2.2'
CocoaPods版本需要 0.36 or later
然后我打開終端搜索ObjectMapper模暗,輸入pod search objectmapper
結(jié)果顯示最新的ObjectMapper版本號是1.3.0
(LOL顯然不對,官方說最新的2.2念祭,但是對CocoaPods版本需求是0.36 or later)
應(yīng)該是CocoaPods版本問題兑宇,升級CocoaPods!
終端輸入:$ sudo gem update cocoapods
pod install
終端輸入:
1粱坤、cd 你的項目profile文件路徑
2隶糕、pod install
到這里,發(fā)現(xiàn)返回
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.
網(wǎng)上資料說站玄,Podfile不要使用TextEdit(文本形式編輯)枚驻,會出現(xiàn)問題,請盡量使用xcode或者終端編輯株旷。
- 可能是語法問題再登?
解決方法:
還是使用TextEdit打開Podfile,語法更改如下
platform :ios,’8.0’
#兼容swift庫
use_frameworks!
#導(dǎo)入所需庫
target 'SmartHome' do
pod 'AFNetworking'
pod 'ObjectMapper', '~> 2.2.0'
end
#適配當(dāng)前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搜了發(fā)現(xiàn)要在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類庫的版本沈撞,與當(dāng)前xcode的swift版本不一致
解決方法:
#適配當(dāng)前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