# 02.使用Carthage替代CocoaPods

> 官方地址: <https://github.com/Carthage/Carthage> 参考文章: <https://www.jianshu.com/p/76b9ff09f99c>

## 集成

### 通过 `Homebrew` 安装

```swift
brew install carthage
```

### 项目配置文件

和使用`CocoaPods`一样, 这里也需要一个无后缀的配置文件 `Carthfile`

#### 配置依赖库

熟悉`Podfile`的话这里就不会陌生了

```swift
github "Alamofire/Alamofire"
```

* 对于`github`指名后会去`github`上找对对应的库&#x20;
  * 格式: `github Username/ProjectName`&#x20;
* `git` 支持`http://` `git://` `ssh://`

> `==` 毫无疑问是说必须为指定版本，如果没有，就不下载编译； `>=` 使用大于或等于指定版本的库，如果有最新，则使用最新的； `~>` 则是一个开区间，如“\~>1.1.0”则会返回“1.1.1\~1.9.9”之间的版本，不包括2.0.0；

## 编译第三方库

类似`pod install`操作, `cd` 到工程目录执行下面命令

```swift
carthage update --platform iOS
```

* 一般这里指定一下平台好些,否则会编译出mac,tv,iOS个平台的,完全没必要
* 执行完成后会看到有个`Carthage`目录 ![](http://ohfpqyfi7.bkt.clouddn.com/15179696493656.jpg)
  * `build`中就是编译好的第三方库
  * `Checkouts`中是第三方库的缓存

## 导入第三方库

手动将`Carthage/Build`中的`framework`加到项目中 ![](http://ohfpqyfi7.bkt.clouddn.com/15179703205983.jpg) ![](http://ohfpqyfi7.bkt.clouddn.com/15179703379835.jpg) ![](http://ohfpqyfi7.bkt.clouddn.com/15179703670579.jpg) 先在就阔以在项目中使用`Alamofire`咯

## 一些坑

### 01: has no shared framework schemes

```swift
*** Skipped building R.swift due to the error:
Dependency "R.swift" has no shared framework schemes for any of the platforms: iOS
```

* 因为`Carthage`需要库工程的`Scheme`需要为`Shared`
* 没有的话可能是因为`git`忽略了相应文件 `xcuserdata`

参考

> <https://stackoverflow.com/questions/35054788/carthage-no-shared-framework-schemes-for-ios-platform-for-my-own-framework> <https://www.jianshu.com/p/a7defcf2e327>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/swift/shi-yong-carthage-ti-dai-cocoapods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
