7

目标

  • 我想CKError在应用程序中向用户显示遇到的问题作为警报。
  • 所以我想从可以显示给用户的错误中提取字符串。

注意:此问题与要显示的 UI 代码无关。只想从错误中提取有意义的字符串。

我尝试使用本地化描述,但它似乎不包含适当的字符串

代码:

以下是我所做的尝试:

po error  
<CKError 0x1c464cea0: "Network Unavailable" (3/NSURLErrorDomain:-1009); "The Internet connection appears to be offline.">  

po error.localizedDescription  
"The operation couldn’t be completed. (CKErrorDomain error 3.)"  

po (error as! CKError).errorUserInfo  
▿ 2 elements  
  ▿ 0 : 2 elements  
    - key : "NSUnderlyingError"  
    - value : Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https:/  
  ▿ 1 : 2 elements  
    - key : "NSDebugDescription"  
    - value : NSURLErrorDomain: -1009  



po (error as? NSError)?.localizedFailureReason  
nil  

po (error as? NSError)?.localizedRecoverySuggestion  
nil  

po (error as? NSError)?.localizedRecoveryOptions  
nil  

po (error as? NSError)?.debugDescription  
▿ Optional<String>  
  - some : "<CKError 0x1c064eaf0: \"Network Unavailable\" (3/NSURLErrorDomain:-1009); \"The Internet connection appears to be offline.\">"  

问题:

调试描述似乎最接近我想要的。

  1. 我错过了什么吗?
  2. 提取可以显示给用户的错误字符串的正确方法是什么?
4

3 回答 3

5

看起来 errorUserInfo[NSUnderlyingError] 中还有另一个错误。尝试从该错误中获取本地化描述。

所以,那将是:

((error as? CKError)?.errorUserInfo[NSUnderlyingErrorKey] as? NSError)?.localizedDescription
于 2018-04-19T16:20:52.197 回答
4

error.localizedDescription实际上是您必须从错误本身处理的所有内容。

您的应用程序可以通过检查错误代码并向用户提供自己的消息来提供更好的错误消息(更加用户友好、本地化等)。

(error as? NSError)?.code
于 2018-04-19T17:01:01.177 回答
1

我并不为此感到自豪,但这就是我所采取的措施。一定会有更好的办法!

public func ckErrorCodeToText(code: CKError.Code) -> String {
    switch code {
    case .alreadyShared: return "alreadyShared"
    case .internalError: return "internalError"
    case .partialFailure: return "partialFailure"
    case .networkUnavailable: return "networkUnavailable"
    case .networkFailure: return "networkFailure"
    case .badContainer: return "badContainer"
    case .serviceUnavailable: return "serviceUnavailable"
    case .requestRateLimited: return "requestRateLimited"
    case .missingEntitlement: return "missingEntitlement"
    case .notAuthenticated: return "notAuthenticated"
    case .permissionFailure: return "permissionFailure"
    case .unknownItem: return "unknownItem"
    case .invalidArguments: return "invalidArguments"
    case .resultsTruncated: return "resultsTruncated"
    case .serverRecordChanged: return "serverRecordChanged"
    case .serverRejectedRequest: return "serverRejectedRequest"
    case .assetFileNotFound: return "assetFileNotFound"
    case .assetFileModified: return "assetFileModified"
    case .incompatibleVersion: return "incompatibleVersion"
    case .constraintViolation: return "constraintViolation"
    case .operationCancelled: return "operationCancelled"
    case .changeTokenExpired: return "changeTokenExpired"
    case .batchRequestFailed: return "batchRequestFailed"
    case .zoneBusy: return "zoneBusy"
    case .badDatabase: return "badDatabase"
    case .quotaExceeded: return "quotaExceeded"
    case .zoneNotFound: return "zoneNotFound"
    case .limitExceeded: return "limitExceeded"
    case .userDeletedZone: return "userDeletedZone"
    case .tooManyParticipants: return "tooManyParticipants"
    case .referenceViolation: return "referenceViolation"
    case .managedAccountRestricted: return "managedAccountRestricted"
    case .participantMayNeedVerification: return "participantMayNeedVerification"
    case .serverResponseLost: return "serverResponseLost"
    case .assetNotAvailable: return "assetNotAvailable"
    @unknown default: return String(code.rawValue)
    }
}
于 2021-04-22T18:17:17.077 回答