Xcode8—Swift开发使用Cocoapods引入第三方库异常处理方法

首先要说下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, 如图:

xcode截图

这时候在运行工程,应该就OK了。。

4、如果还是不行,试试将工程Display Name 补全。默认是空的,按提示补全它。


xcode截图

直接按着提示键入:“testSwiftCocoaPods”即可。。

以上处理应该就可以成功引入第三方库并且运行了,如果还是不可以,提出来一起研究下。

其他问题及解决方法可以参考以下信息:

参考:stackoverflow 以及 Alamofire issues

你可能感兴趣的:(Xcode8—Swift开发使用Cocoapods引入第三方库异常处理方法)