Carthage 安裝使用
1.使用Homebrew安裝Carthage之前,先對其進行更新,不然可能會安裝到比較老的版本。
sudo brew update
2.安裝Carthage
方法1
sudo brew install carthage (這種方式較好)
方法2
下載安裝Carthage.pkg(國內基本下不下來)
方法3
下載Carthage,運行make install。
3.查看及升級Carthage版本
查看:carthage version
升級:brew upgrade carthage
4.卸載Carthage
sudo brew uninstall carthage
5.創建空的Cartfile文件
touch Cartfile
6.使用Xcode命令打開Cartfile文件
open -a Xcode Cartfile
7.添加依賴庫
例:github "Alamofire/Alamofire" ~> 3.0
- 版本的含義: ~>3.0:表示使用版本3.0以上但是低于4.0的最新版本,如3.5, 3.9。 ==3.0:表示使用3.0版本。 >=3.0:表示使用3.0或更高的版本。如果你沒有指明版本號,則會自動使用最新的版本。
8.保存并關閉Cartfile文件并執行carthage update
執行update命令后,你的項目目錄結構大致如下:
Cartfile:文件用來標注你需要哪些依賴庫,對應版本或者 Git 分支(需要提交到 Git)。
Cartfile.resolved:文件用來跟蹤項目當前所用的依賴版本號,為了保持多端開發一致(需要提交到 Git)。
Carthage:文件夾用來存放依賴庫的源文件和編譯后的文件(不需要提交到 Git,可以修改.gitignore文件,增加忽略 Carthage 文件夾就行了:#Carthage Carthage)。
備注:
Cartfile利用xcode-select命令來編譯Framework,如果你想用其他版的Xcode進行編譯,執行下面這條命令,把xcode-select的路徑改為另一版本Xcode就可以。sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
9.引入Framework
- 設置Xcode自動搜索Framework的目錄 Target—>Build Setting—>Framework Search Path—>添加路徑"$(SRCROOT)/Carthage/Build/iOS"
- 添加編譯的額外腳本 Target—>Build Phases —>”+”—>New Run Script Phase—>添加腳本"/usr/local/bin/carthage copy-frameworks" 3. 添加文件 Input Files—>添加路徑"$(SRCROOT)/Carthage/Build/iOS/庫名.framework"
以下是我安裝時出現的一些錯誤。
錯誤1:
Error: Could not symlink bin/carthage/usr/local/bin is not writable.解決方案:sudo brew remove carthagesudo brew install carthage
錯誤2:
Warning: You are using OS X 10.11.We do not provide support for this pre-release version.You may encounter build failures or other breakage.解決方案:sudo brew update
錯誤3:
The following build commands failed:CompileSwift normal arm64 /Users/XX/Carthage/Checkouts/Alamofire/Source/Timeline.swiftCompileSwift normal arm64 /Users/XX/Carthage/Checkouts/Alamofire/Source/Upload.swiftCompileSwift normal arm64 /Users/XX/Carthage/Checkouts/Alamofire/Source/ParameterEncoding.swiftCompileSwift normal arm64 /Users/XX/Carthage/Checkouts/Alamofire/Source/Request.swiftCompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler(5 failures)解決方案:如果升級Carthage和brew都不行,可以升級下Xcode。