首先要說下Cocoapods 的Podfile文件的格式問題:
swift里引用必須要加入use_frameworks!
這句,否則不能成功。因?yàn)闆]有加這句它默認(rèn)生成的是.a文件,加了這句才會生成.framework 文件。
完整的格式:
platform :ios, '10.0'
use_frameworks!
target 'YourTarget' do
pod 'Alamofire', '~>4.0.0'
pod 'SwiftyJSON'
end
當(dāng)你Cocoapods會使用之后,直接就引入第三方庫,特別是現(xiàn)在swift版本還不是很完善,可能會遇見一些問題,下面我就說一下我遇見的問題及其解決辦法。
當(dāng)你寫完P(guān)odfile 文件, pod install 成功之后,去打開工程文件.xcworkspace運(yùn)行, 這時候它一般會提示你更新到swift最新版本,建議不要convert,選擇“l(fā)ater”吧,因?yàn)槟慵词垢铝艘惨欢芡ㄟ^,可能報錯更多。這是因?yàn)閟wift版本升級,棄用的方法太多,不兼容導(dǎo)致!
當(dāng)我引用網(wǎng)絡(luò)請求Alamofire 就出現(xiàn)了異常
假如報錯:
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版本不兼容,讓你升級swift版本信息。
解決方法:
1、首先檢查你的cocoapods是不是最新版本,
pod --version
查看下版本
如果不是最新版本,升級它
sudo gem update cocoapods
如果你遇見問題或是不太會升級,可以參考:Swift - CocoaPods的安裝使用詳解這篇文章。
2、在Podfile文件后面追加下面的代碼:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0'
end
end
end
完整樣子:
platform :ios, '10.0'
use_frameworks!
target 'testSwiftCocaPods' do
pod 'Alamofire'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.0'
end
end
end
在pod install ,成功之后在運(yùn)行。
3、假如還報錯,像下面的錯誤:
ld: /Users/Qianhan/Library/Developer/Xcode/DerivedData/testSwiftCocaPods-fokbuwlqlljkizfhxdqvjgjqbtwe/Build/Products/Debug-iphonesimulator/Alamofire/Alamofire.framework/Alamofire compiled with newer version of Swift language (3.0) than previous files (2.0) for architecture x86_64
clang: error: unable to execute command: Segmentation fault: 11
clang: error: linker command failed due to signal (use -v to see invocation)
這時候要去修改一下Swift Language Version選項(xiàng),將YES改成NO, 如圖:
這時候在運(yùn)行工程,應(yīng)該就OK了。。
4、如果還是不行,試試將工程Display Name 補(bǔ)全。默認(rèn)是空的,按提示補(bǔ)全它。
直接按著提示鍵入:“testSwiftCocoaPods”即可。。
以上處理應(yīng)該就可以成功引入第三方庫并且運(yùn)行了,如果還是不可以,提出來一起研究下。