swift的异常处理:本质是错误信息的传递方式
Posted feng9exe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift的异常处理:本质是错误信息的传递方式相关的知识,希望对你有一定的参考价值。
func response(from resultObject: Any) throws -> Response {
guard
let dict = resultObject as? [String: AnyObject],
let transaction = PendingTransaction.from(dict)
else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
return transaction
}
func convertPrivateKeyToKeystoreFile(privateKey: String, passphrase: String) -> Result<[String: Any], KeystoreError> {
guard let data = Data(hexString: privateKey) else {
return .failure(KeystoreError.failedToImportPrivateKey)
}
do {
let key = try KeystoreKey(password: passphrase, key: data)
let data = try JSONEncoder().encode(key)
let dict = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
return .success(dict)
} catch {
return .failure(KeystoreError.failedToImportPrivateKey)
}
}
var asDictionary: [String: Any]? {
if let data = self.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
return [:]
}
}
return [:]
}
以上是关于swift的异常处理:本质是错误信息的传递方式的主要内容,如果未能解决你的问题,请参考以下文章