Swift 5: success/failure 块中的错误“'catch' 块无法访问,因为 'do' 块中没有抛出任何错误”
Swift 5: Error in success/failure block "'catch' block is unreachable because no errors are thrown in 'do' block"
我正在编写一个带有错误处理的闭包,但我在捕获行中收到警告“'catch' 块无法访问,因为 'do' 块中没有抛出任何错误”。
这是我的代码:
class Service {
func graphQL(body: [String:Any], onSuccess: @escaping (Foundation.Data) -> (), onFailure: @escaping (Error) -> ()) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
onFailure(error)
}
if let data = data {
do{
onSuccess(data)
}
catch{
onFailure(error)
}
}
}.resume()
}
class TimeDepositManager {
func getTimeDeposits(onSuccess: @escaping ([TimeDeposits]) -> (), onFailure: @escaping (Error) -> ()) {
let body = ["query": "{ account { timeDeposits { returnAmount interestRate dueDate originalAmount id startDate currency } } }"
]
Service().graphQL(body: body, onSuccess: { data in
let json = try? JSONDecoder().decode(GraphQLResponse.self, from: data)
onSuccess(json?.data?.account?.timeDeposits ?? [])
}, onFailure: { error in
print(error)
})
}
我想要 func getTimeDeposits() 中的 onSuccess 逻辑,但是如何删除这个 catch 块警告?
how remove this catch block warning?
通过删除 do-catch
块完全因为没有任何内容 throws
替换
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
onFailure(error)
}
if let data = data {
do{
onSuccess(data)
}
catch{
onFailure(error)
}
}
}.resume()
和
URLSession.shared.dataTask(with: request) { (data, _, error) in
if let error = error {
onFailure(error)
} else {
onSuccess(data!)
}
}.resume()
旁注:而不是非常繁琐和过时
onSuccess: @escaping (Foundation.Data) -> (), onFailure: @escaping (Error) -> ())
考虑使用 Result
类型
completion: @escaping (Result<Data,Error>) -> Void)
Swift 在错误处理方面需要一些“期望”。
即success
需要声明它可以throw
报错(根据我对你的要求的理解),例如
func graphQL(body: [String:Any], onSuccess: @escaping (Foundation.Data) throws -> (), onFailure: @escaping (Error) -> ()) {
然后您需要 try
并调用 success
...
do{
try onSuccess(data)
}
catch{
onFailure(error)
}
我正在编写一个带有错误处理的闭包,但我在捕获行中收到警告“'catch' 块无法访问,因为 'do' 块中没有抛出任何错误”。 这是我的代码:
class Service {
func graphQL(body: [String:Any], onSuccess: @escaping (Foundation.Data) -> (), onFailure: @escaping (Error) -> ()) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
onFailure(error)
}
if let data = data {
do{
onSuccess(data)
}
catch{
onFailure(error)
}
}
}.resume()
}
class TimeDepositManager {
func getTimeDeposits(onSuccess: @escaping ([TimeDeposits]) -> (), onFailure: @escaping (Error) -> ()) {
let body = ["query": "{ account { timeDeposits { returnAmount interestRate dueDate originalAmount id startDate currency } } }"
]
Service().graphQL(body: body, onSuccess: { data in
let json = try? JSONDecoder().decode(GraphQLResponse.self, from: data)
onSuccess(json?.data?.account?.timeDeposits ?? [])
}, onFailure: { error in
print(error)
})
}
我想要 func getTimeDeposits() 中的 onSuccess 逻辑,但是如何删除这个 catch 块警告?
how remove this catch block warning?
通过删除 do-catch
块完全因为没有任何内容 throws
替换
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
onFailure(error)
}
if let data = data {
do{
onSuccess(data)
}
catch{
onFailure(error)
}
}
}.resume()
和
URLSession.shared.dataTask(with: request) { (data, _, error) in
if let error = error {
onFailure(error)
} else {
onSuccess(data!)
}
}.resume()
旁注:而不是非常繁琐和过时
onSuccess: @escaping (Foundation.Data) -> (), onFailure: @escaping (Error) -> ())
考虑使用 Result
类型
completion: @escaping (Result<Data,Error>) -> Void)
Swift 在错误处理方面需要一些“期望”。
即success
需要声明它可以throw
报错(根据我对你的要求的理解),例如
func graphQL(body: [String:Any], onSuccess: @escaping (Foundation.Data) throws -> (), onFailure: @escaping (Error) -> ()) {
然后您需要 try
并调用 success
...
do{
try onSuccess(data)
}
catch{
onFailure(error)
}