【问题标题】:CKError localizedDescriptionCKError 本地化描述
【发布时间】:2018-09-26 03:09:45
【问题描述】:

目标

  • 我想在应用程序中向用户显示遇到的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. 提取可以显示给用户的错误字符串的正确方法是什么?

【问题讨论】:

  • 尝试(错误为!CKError).localizedDescription
  • 与不投射效果相同
  • 我认为最好的方法是将错误代码映射到错误消息。我相信这是 Apple 推荐的,但不记得我在哪里看到的。
  • @DuncanGroenewald 我同意,如果我们想要相关消息,我认为自定义消息是唯一的选择。这绝对可以改进,可能是提出错误的好主意

标签: ios swift cloudkit nserror ckerror


【解决方案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)
    }
}

【讨论】:

    【解决方案2】:

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

    所以,那就是:

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

    【讨论】:

    • 它返回它返回Optional(Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https://gateway.icloud.com:443/ckdatabase/api/client/query/retrieve, NSErrorFailingURLKey=https://gateway.icloud.com:443/ckdatabase/api/client/query/retrieve, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.}) 这对用户来说太技术性了
    • 我只是觉得这是CKError的一个错误,localizedDescription没有适当填充。
    • 您评论中的本地化描述是“Internet 连接似乎处于脱机状态。”。这就是你需要的。
    • 它不是一个单独的键NSLocalizedDescription,它是长错误消息的一小部分。所以这样提取是不可靠的。
    • 不,里面有一个单独的错误。我用代码更新了我的答案,以帮助你把它拉出来。
    【解决方案3】:

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

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

    (error as? NSError)?.code
    

    【讨论】:

    • 不幸的是,本地化描述没有提供有意义的描述。处理我自己的错误消息的问题是有 26 个错误代码。如果我支持 5 种语言,那么它就是 26 x 5 = 130 条错误消息。可悲的是,他们在调试描述中确实有错误,但有技术信息。我认为这更像是一个错误,想知道是否有解决方案
    猜你喜欢
    • 2014-04-15
    • 2015-01-26
    • 1970-01-01
    • 2013-06-27
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2011-11-15
    相关资源
    最近更新 更多