Swift3.0 與 Alamofire 編譯

本文主要講述,如何使用Xcode-beta 3編譯Swift 3.0工程,并通過Cocoapods引入Alamofire

環(huán)境

  • OS X EI 10.11.6
  • Xcode-beta 3

準(zhǔn)備工作

  1. 首先更新cocoapodspre版本:
sudo gem install -n /usr/local/bin cocoapods --pre

這里因?yàn)橄到y(tǒng)權(quán)限保護(hù),所以需要指定安裝位置為/usr/local/bin
成功后檢查版本

pod --version

應(yīng)該是

1.1.0.beta.1
  1. 創(chuàng)建xcoodeproj,打開Xcode-beta 3,選擇File-new-project,找到iOS-Single View Application,命名,這里叫做SwiftAlamofireDemo,下面語言選擇swift,其他隨意。
  2. 編譯新建的SwiftAlamofireDemo,可以正常運(yùn)行。
  3. 終端,進(jìn)入到工程對應(yīng)目錄:
?  SwiftAlamofireDemo git:(master) ? ls
SwiftAlamofireDemo           SwiftAlamofireDemo.xcodeproj
  1. 創(chuàng)建Podfile
?  SwiftAlamofireDemo git:(master) ? pod init
?  SwiftAlamofireDemo git:(master) ? ls
Podfile                      SwiftAlamofireDemo           SwiftAlamofireDemo.xcodeproj
  1. Xcode-beta 3編輯Podfile,內(nèi)容為:
platform :ios, '9.0'
use_frameworks!
target 'SwiftAlamofireDemo' do
pod 'Alamofire',
:git => 'https://github.com/Alamofire/Alamofire.git',
:branch => 'swift3'
end
//末尾需要有一個(gè)空行,這里 markdown 原因不顯示

這里的意思是,使用swift3分支,因?yàn)?code>master分支并不支持最新的swift 3.0語法,需要進(jìn)行convert,會(huì)出一堆問題,很麻煩而且不能解決的很漂亮,所以直接使用指定分支。

  1. 安裝Pods,這里注意跳過庫更新,不然配置不好的話,就得無限卡住了:
?  SwiftAlamofireDemo git:(master) ? pod install --verbose --no-repo-update 
  Preparing

Analyzing dependencies

Inspecting targets to integrate
  Using `ARCHS` setting to build architectures of target `Pods-SwiftAlamofireDemo`: (``)

Fetching external sources
-> Pre-downloading: `Alamofire` from `https://github.com/Alamofire/Alamofire.git`, branch `swift3`
  $ /usr/bin/git ls-remote https://github.com/Alamofire/Alamofire.git swift3
  304cb54998ab56765b8bd638c6c8bfc9a0bf6756  refs/heads/swift3
  > Copying Alamofire from
  `/Users/Dev/Library/Caches/CocoaPods/Pods/External/Alamofire/9da42fbc76763f2067b3b8cc71c2e406` to
  `Pods/Alamofire`

Resolving dependencies of `Podfile`

Comparing resolved specification to the sandbox manifest
  A Alamofire

Downloading dependencies

-> Installing Alamofire (3.4.1)
  - Running pre install hooks

Generating Pods project
  - Creating Pods project
  - Adding source files to Pods project
  - Adding frameworks to Pods project
  - Adding libraries to Pods project
  - Adding resources to Pods project
  - Linking headers
  - Installing targets
    - Installing target `Alamofire` iOS 8.0
      - Generating Info.plist file at `Pods/Target Support Files/Alamofire/Info.plist`
      - Generating module map file at `Pods/Target Support Files/Alamofire/Alamofire.modulemap`
      - Generating umbrella header at `Pods/Target Support Files/Alamofire/Alamofire-umbrella.h`
    - Installing target `Pods-SwiftAlamofireDemo` iOS 9.0
      - Generating Info.plist file at `Pods/Target Support
      Files/Pods-SwiftAlamofireDemo/Info.plist`
      - Generating module map file at `Pods/Target Support
      Files/Pods-SwiftAlamofireDemo/Pods-SwiftAlamofireDemo.modulemap`
      - Generating umbrella header at `Pods/Target Support
      Files/Pods-SwiftAlamofireDemo/Pods-SwiftAlamofireDemo-umbrella.h`
  - Running post install hooks
  - Writing Xcode project file to `Pods/Pods.xcodeproj`
    - Generating deterministic UUIDs
  - Writing Lockfile in `Podfile.lock`
  - Writing Manifest in `Pods/Manifest.lock`

Integrating client project

[!] Please close any current Xcode sessions and use `SwiftAlamofireDemo.xcworkspace` for this project from now on.

Integrating target `Pods-SwiftAlamofireDemo` (`SwiftAlamofireDemo.xcodeproj` project)
  Adding Build Phase '[CP] Embed Pods Frameworks' to project.
  Adding Build Phase '[CP] Copy Pods Resources' to project.
  Adding Build Phase '[CP] Check Pods Manifest.lock' to project.
  - Running post install hooks
    - cocoapods-stats from
    `/Library/Ruby/Gems/2.0.0/gems/cocoapods-stats-1.0.0/lib/cocoapods_plugin.rb`
  Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

這時(shí)候我們發(fā)現(xiàn):

?  SwiftAlamofireDemo git:(master) ? ls
Podfile                        Pods                           SwiftAlamofireDemo.xcodeproj
Podfile.lock                   SwiftAlamofireDemo             SwiftAlamofireDemo.xcworkspace

到這里,部署就完成了。

調(diào)試

關(guān)閉xcodeproj,然后用Xcode-beta 3打開xcworkspace。

這時(shí)候編譯運(yùn)行,有的可以成功,有的會(huì)報(bào)錯(cuò):

dyld: Library not loaded: @rpath/Alamofire.framework/Alamofire
  Referenced from: /Users/Dev/Library/Developer/CoreSimulator/Devices/xx/data/Containers/Bundle/Application/xx/SwiftAlamofireDemo.app/SwiftAlamofireDemo
  Reason: image not found

解決方案:

SwiftAlamofireDemo Target - General - Embedded Binaries 增加 Alamofire.framework
SwiftAlamofireDemo Target - General - Linked Frameworks and Libraries 增加 Alamofire.framework
SwiftAlamofireDemo Target - Build Phrase 中 Linked Frameworks and Libraries 增加 Alamofire.framework

之后就可以成功編譯。至于庫的使用,參見Github

資料

image not found解決方案:[https://github.com/Alamofire/Alamofire/issues/101]
Cocoapods 更新 pre資料:[https://github.com/Alamofire/Alamofire/issues/1316]
Swift3源碼:[https://github.com/Alamofire/Alamofire/tree/swift3]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容