在 do-try-catch 中解码到另一个 class 会产生编译错误
Decoding to another class in do-try-catch produces compile error
我遇到了编译错误。
我试图将 API 响应解码为自定义 class(实际上是结构)。
我们的 API returns 几种类型的错误(有时它包含字符串,但有时它包含 returns 代码(元组 [String: Int]))
所以如果第一次解码失败,我想解码成另一个class。
如果我尝试解码成一个 class,没关系,但如果我尝试解码成 catch
范围内的另一个 class,它会产生编译错误
Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'
你知道怎么解决吗?
- 我正在使用
Swift5
、Xcode 12.4
struct ErrorInfo: Codable {
public let message: String?
public let details: String?
public let debugInformation: String?
}
struct ErrorInfoWithErrorCode: Codable {
public let message: [String: Int] // this is the difference.
public let details: String?
public let debugInformation: String?
}
let request = URLRequest(url: URL(string: "https://www.google.com/")!)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpResponse = response as? HTTPURLResponse, !(200..<300).contains(httpResponse.statusCode) {
do {
let errorMessage = try decoder.decode(ErrorInfo.self, from: data)
print(errorMessage)
} catch {
let errorMessage = try decoder.decode(ErrorInfoWithErrorCode.self, from: data)
print(errorMessage)
} catch {
fatalError()
}
}
}
task.resume()
playground 中的错误消息。
error: MyPlayground.playground:28:54: error: invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'
let task = URLSession.shared.dataTask(with: request) { data, response, error in
另一个嵌套try
也需要一个do
,你可以试试
do {
let errorMessage = try decoder.decode(ErrorInfo.self, from: data)
print(errorMessage)
} catch {
do {
let errorMessage = try decoder.decode(ErrorInfoWithErrorCode.self, from: data)
print(errorMessage)
} catch {
fatalError()
}
}
我遇到了编译错误。 我试图将 API 响应解码为自定义 class(实际上是结构)。 我们的 API returns 几种类型的错误(有时它包含字符串,但有时它包含 returns 代码(元组 [String: Int])) 所以如果第一次解码失败,我想解码成另一个class。
如果我尝试解码成一个 class,没关系,但如果我尝试解码成 catch
范围内的另一个 class,它会产生编译错误
Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'
你知道怎么解决吗?
- 我正在使用
Swift5
、Xcode 12.4
struct ErrorInfo: Codable {
public let message: String?
public let details: String?
public let debugInformation: String?
}
struct ErrorInfoWithErrorCode: Codable {
public let message: [String: Int] // this is the difference.
public let details: String?
public let debugInformation: String?
}
let request = URLRequest(url: URL(string: "https://www.google.com/")!)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpResponse = response as? HTTPURLResponse, !(200..<300).contains(httpResponse.statusCode) {
do {
let errorMessage = try decoder.decode(ErrorInfo.self, from: data)
print(errorMessage)
} catch {
let errorMessage = try decoder.decode(ErrorInfoWithErrorCode.self, from: data)
print(errorMessage)
} catch {
fatalError()
}
}
}
task.resume()
playground 中的错误消息。
error: MyPlayground.playground:28:54: error: invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'
let task = URLSession.shared.dataTask(with: request) { data, response, error in
另一个嵌套try
也需要一个do
,你可以试试
do {
let errorMessage = try decoder.decode(ErrorInfo.self, from: data)
print(errorMessage)
} catch {
do {
let errorMessage = try decoder.decode(ErrorInfoWithErrorCode.self, from: data)
print(errorMessage)
} catch {
fatalError()
}
}