CKError localizedDescription

CKError localizedDescription

瞄准

注:本题不涉及UI代码显示。只想从错误中提取有意义的字符串。

我尝试使用 localizedDescription,但它似乎不包含合适的字符串

代码:

以下是我的尝试:

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. 提取可以显示给用户的错误字符串的正确方法是什么?

看起来错误 UserInfo[NSUnderlyingError] 中还有另一个错误。尝试从该错误中获取 localizedDescription。

所以,那就是:

((error as? CKError)?.errorUserInfo[NSUnderlyingErrorKey] as? NSError)?.localizedDescription

error.localizedDescription 是您真正需要处理的错误本身。

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

(error as? NSError)?.code

我并不以此为荣,但这就是我所使用的。一定有更好的方法!

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)
    }
}