Response 0.0.2

Response 0.0.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新版本2017年8月
SwiftSwift 版本4.0
SPM支持 SPM

Valery Fomenko维护。



Response 0.0.2

  • Valery Fomenko

异步回调与标准 Swift 错误处理冲突。

Response 帮助解决这个问题。

使用完成处理器的传统接口看起来像这样

protocol MessageService {
    func get(id: String, completionHandler: (Message?, Error?) -> Void)
}

let messages: MessageService = //
messages.get(id: "123") { message, error in
    // implicit convention that there are two valid combinations
    // (message, nil)
    // (nil, error)
        
    // Swift error handling is not applicable
        
    if let message = message {
        //
    } else if let error = error {
        //
    } else {
        // ??
    }
}

使用 Response 的相同示例

import Response

protocol MessageService {
    func get(id: String, responseHandler: Response<Message>.Handler)
}

let messages: MessageService = //
messages.get(id: "123") { response in
    do {
        let message = try response.result()
        //
    } catch {
        //
    }
}