我正在尝试检查error.localizedDescription
是否包含某个string
,但我一直崩溃
if error.localizedDescription.contains("\"api.error.cardRejected.2000\"") {
failCompletion()
}
我什至尝试使用另一种方法
if let description = (error! as NSError).userInfo[NSLocalizedDescriptionKey] as? String {
if description.contains("api.error.cardRejected.2000") {
failCompletion()
}
}
我仍然在日志中说同样的崩溃
-[__NSDictionaryM domain]: unrecognized selector sent to instance 0x60000046b520
它在我使用debugDescription
进行检查时有效,但是我想使用localizedDecription
进行检查,因为调试仅在调试时有效
NSError本地化描述是从内部自动生成的,这是API告诉的内容:
/* The primary user-presentable message for the error, for instance for NSFileReadNoPermissionError: "The file "File Name" couldn't be opened because you don't have permission to view it.". This message should ideally indicate what failed and why it failed. This value either comes from NSLocalizedDescriptionKey, or NSLocalizedFailureErrorKey+NSLocalizedFailureReasonErrorKey, or NSLocalizedFailureErrorKey. The steps this takes to construct the description include:
1. Look for NSLocalizedDescriptionKey in userInfo, use value as-is if present.
2. Look for NSLocalizedFailureErrorKey in userInfo. If present, use, combining with value for NSLocalizedFailureReasonErrorKey if available.
3. Fetch NSLocalizedDescriptionKey from userInfoValueProvider, use value as-is if present.
4. Fetch NSLocalizedFailureErrorKey from userInfoValueProvider. If present, use, combining with value for NSLocalizedFailureReasonErrorKey if available.
5. Look for NSLocalizedFailureReasonErrorKey in userInfo or from userInfoValueProvider; combine with generic "Operation failed" message.
6. Last resort localized but barely-presentable string manufactured from domain and code. The result is never nil.
*/
open var localizedDescription: String { get }
因此,它崩溃了(可能在步骤6。),则此NSError的构造不正确-因此,查找其构造方式和原因,可能是在底层错误的某个层上,userInfo的某些键意外地设置为NSDictionary
而不是[ C0]。