目标
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.\">" 问题:
调试描述似乎最接近我想要的。
发布于 2018-04-19 16:20:52
看来errorUserInfoNSUnderlyingError中还有另一个错误。尝试从该错误中获取localizedDescription。
因此,这将是:
((error as? CKError)?.errorUserInfo[NSUnderlyingErrorKey] as? NSError)?.localizedDescription发布于 2018-04-19 17:01:01
error.localizedDescription实际上是您从错误本身中需要处理的全部内容。
您的应用程序可以提供更好的错误信息(更方便用户,本地化,等等)通过检查错误代码并向用户提供自己的消息。
(error as? NSError)?.code发布于 2021-04-22 18:17:17
我并不以此为荣,但这正是我所诉诸的。一定有更好的办法!
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)
}
}https://stackoverflow.com/questions/49856270
复制相似问题