首先要說下Cocoapods 的Podfile文件的格式問題:
swift里引用必須要加入use_frameworks!
這句,否則不能成功。因為沒有加這句它默認生成的是.a文件,加了這句才會生成.framework 文件。
完整的格式:
platform :ios, '10.0'
use_frameworks!
target 'YourTarget' do
pod 'Alamofire', '~>4.0.0'
pod 'SwiftyJSON'
end
當你Cocoapods會使用之后,直接就引入第三方庫,特別是現在swift版本還不是很完善,可能會遇見一些問題,下面我就說一下我遇見的問題及其解決辦法。
當你寫完Podfile 文件, pod install 成功之后,去打開工程文件.xcworkspace運行, 這時候它一般會提示你更新到swift最新版本,建議不要convert,選擇“later”吧,因為你即使更新了也一定能通過,可能報錯更多。這是因為swift版本升級,棄用的方法太多,不兼容導致!
當我引用網絡請求Alamofire 就出現了異常
假如報錯:
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 ,成功之后在運行。
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選項,將YES改成NO, 如圖:
這時候在運行工程,應該就OK了。。
4、如果還是不行,試試將工程Display Name 補全。默認是空的,按提示補全它。
直接按著提示鍵入:“testSwiftCocoaPods”即可。。
以上處理應該就可以成功引入第三方庫并且運行了,如果還是不可以,提出來一起研究下。