📜  从完成中快速获取错误 - Swift 代码示例

📅  最后修改于: 2022-03-11 15:00:59.904000             🧑  作者: Mango

代码示例3
func asynchronousWork(completion: (inner: () throws -> [String: Any]) -> Void) -> Void {
    URLConnection.sendAsynchronousRequest(request, queue: queue) { 
        (response, data, error) -> Void in
        guard let data = data else { return }
        do {
            guard let result = try JSONSerialization.JSONObjectWithData(data, options: []) 
                as? [String: Any] else {
                completion(inner: { throw OurError.InvalidJSONType })
            }
            completion(inner: { return result })
        } catch let error {
            completion(inner: { throw error })
        }
    }
}

// Call
asynchronousWork { (inner: () throws -> [String: Any]) -> Void in
    do {
        let result = try inner()
    } catch let error {
        print(error)
    }
}