CombineNetwork
CombineNetwork
为使用Combine.framework
进行网络调用提供了简单的接口。
安装
CocoaPods
在Podfile
中包含以下行:
pod CombineNetwork
Swift包管理器
使用当前GitHub URL添加Package Dependency
。
引用
示例
测试 JSON 文件
https://swapi.co/api
JSON 模型
struct StarWars: Decodable {
let films: String
}
从 JSON 文件中检索数据
let client = APIClient()
let url = URL(string: "https://swapi.dev/api/")!
let subscriber = Subscribers.Sink<StarWars?, APIError>(
receiveCompletion: { print($0) },
receiveValue: { print($0) })
client.requestObject(from: url, using: .get)
.subscribe(subscriber)