本文主要讲述,如何使用Xcode-beta 3
编译Swift 3.0
工程,并通过Cocoapods
引入Alamofire
。
环境
- OS X EI 10.11.6
- Xcode-beta 3
准备工作
- 首先更新
cocoapods
到pre
版本:
sudo gem install -n /usr/local/bin cocoapods --pre
这里因为系统权限保护,所以需要指定安装位置为/usr/local/bin
成功后检查版本
pod --version
应该是
1.1.0.beta.1
- 创建
xcoodeproj
,打开Xcode-beta 3
,选择File-new-project
,找到iOS-Single View Application
,命名,这里叫做SwiftAlamofireDemo
,下面语言选择swift
,其他随意。 - 编译新建的
SwiftAlamofireDemo
,可以正常运行。 - 终端,进入到工程对应目录:
➜ SwiftAlamofireDemo git:(master) ✗ ls
SwiftAlamofireDemo SwiftAlamofireDemo.xcodeproj
- 创建
Podfile
➜ SwiftAlamofireDemo git:(master) ✗ pod init
➜ SwiftAlamofireDemo git:(master) ✗ ls
Podfile SwiftAlamofireDemo SwiftAlamofireDemo.xcodeproj
- 用
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
,会出一堆问题,很麻烦而且不能解决的很漂亮,所以直接使用指定分支。
- 安装
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]