
同样的一个需求,用 AF 实现:
AF.request("https://api.mywebserver.com/v1/board", method: .get, parameters: ["title": "New York Highlights"]) .validate(statusCode: 200..<300) .responseDecodable { (response: DataResponse) in switch response.result { case .success(let board): print("Created board title is \(board.title)") // New York Highlights case .failure(let error): print("Board creation failed with error: \(error.localizedDescription)") } } 用 URLSession 实现:
enum Error: Swift.Error { case requestFailed } // Build up the URL var compOnents= URLComponents(string: "https://api.mywebserver.com/v1/board")! components.queryItems = ["title": "New York Highlights"].map { (key, value) in URLQueryItem(name: key, value: value) } // Generate and execute the request let request = try! URLRequest(url: components.url!, method: .get) URLSession.shared.dataTask(with: request) { (data, response, error) in do { guard let data = data, let respOnse= response as? HTTPURLResponse, (200 ..< 300) ~= response.statusCode, error == nil else { // Data was nil, validation failed or an error occurred. throw error ?? Error.requestFailed } let board = try JSONDecoder().decode(Board.self, from: data) print("Created board title is \(board.title)") // New York Highlights } catch { print("Board creation failed with error: \(error.localizedDescription)") } } 1 pupboss 2021-11-24 10:55:28 +08:00 其实 AF 只是替开发者执行了 let board = try JSONDecoder().decode(Board.self, from: dta) 然后 AF 使用了 Swift 语言特性 Result 类型,上面这个 AF 的例子过于简化了,实际项目也不会这么短的 https://developer.apple.com/documentation/swift/result |
2 ming1016 2021-12-08 20:47:05 +08:00 可以搭配着 Combine 来用,assign 给 ViewModel 的数据属性和 SwiftUI 绑定在一起,响应式交互。 ```swift URLSession.shared.dataTaskPublisher(for: req) .map { data, res in print(String(decoding: data, as: UTF8.self)) return data } .mapError { _ in APISevError.resError } .decode(type: Request.Res.self, decoder: de) .mapError(APISevError.parseError) .receive(on: RunLoop.main) .eraseToAnyPublisher() ``` 绑定部分代码 ```swift repStream = resSubject .map { $0.items } .assign(to: \.repos, on: self) ``` |