Swift3.0 与 Alamofire 编译

本文主要讲述,如何使用Xcode-beta 3编译Swift 3.0工程,并通过Cocoapods引入Alamofire

环境

  • OS X EI 10.11.6
  • Xcode-beta 3

准备工作

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

这里因为系统权限保护,所以需要指定安装位置为/usr/local/bin
成功后检查版本

pod --version

应该是

1.1.0.beta.1
  1. 创建xcoodeproj,打开Xcode-beta 3,选择File-new-project,找到iOS-Single View Application,命名,这里叫做SwiftAlamofireDemo,下面语言选择swift,其他随意。
  2. 编译新建的SwiftAlamofireDemo,可以正常运行。
  3. 终端,进入到工程对应目录:
➜  SwiftAlamofireDemo git:(master) ✗ ls
SwiftAlamofireDemo           SwiftAlamofireDemo.xcodeproj
  1. 创建Podfile
➜  SwiftAlamofireDemo git:(master) ✗ pod init
➜  SwiftAlamofireDemo git:(master) ✗ ls
Podfile                      SwiftAlamofireDemo           SwiftAlamofireDemo.xcodeproj
  1. Xcode-beta 3编辑Podfile,内容为:
platform :ios, '9.0'
use_frameworks!
target 'SwiftAlamofireDemo' do
pod 'Alamofire',
:git => 'https://github.com/Alamofire/Alamofire.git',
:branch => 'swift3'
end
//末尾需要有一个空行,这里 markdown 原因不显示

这里的意思是,使用swift3分支,因为master分支并不支持最新的swift 3.0语法,需要进行convert,会出一堆问题,很麻烦而且不能解决的很漂亮,所以直接使用指定分支。

  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.

这时候我们发现:

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

到这里,部署就完成了。

调试

关闭xcodeproj,然后用Xcode-beta 3打开xcworkspace

这时候编译运行,有的可以成功,有的会报错:

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]

你可能感兴趣的:(Swift3.0 与 Alamofire 编译)