CKError
作为提醒。 注意:此问题与要显示的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.\">"
调试描述似乎与我想要的最接近。
答案 0 :(得分:3)
看起来errorUserInfo [NSUnderlyingError]中还有另一个错误。尝试从该错误中获取localizedDescription。
所以,那将是:
((error as? CKError)?.errorUserInfo[NSUnderlyingErrorKey] as? NSError)?.localizedDescription
答案 1 :(得分:3)
error.localizedDescription
实际上只需要处理错误本身。
您的应用可以通过检查错误代码并向用户提供自己的消息来提供更好的错误消息(更加用户友好,本地化等)。
(error as? NSError)?.code
答案 2 :(得分:0)
我并不为此感到自豪,但这就是我所采取的措施。一定有更好的方法!
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)
}
}