我想我已经在NSError上找到了一个错误,但想要运行它,请给大家看看,看看实际上是我做错了什么。
将说明发送到使用userInfo的NSError实例时出现崩溃。
如果将userInfo设置为nil,说明将按预期工作。
NSString* const domain = @"DOMAIN";
NSError* nsError1 = [NSError errorWithDomain:domain code:1 userInfo:nil]; // Not using Dic
NSLog(@"nsError1 description %@",[nsError1 description]);
NSMutableDictionary* errorDetail = [NSMutableDictionary dictionary];
NSString* underlying = [NSString stringWithFormat:@"Error Domain=%@ Code=%d", NSPOSIXErrorDomain, 1];
[errorDetail setObject:underlying forKey:NSUnderlyingErrorKey];
NSError* nsError2 = [NSError errorWithDomain:domain code:1 userInfo:errorDetail]; // Useing Dic
NSLog(@"nsError2 description %@",[nsError2 description]); // CRASH
输出:
2013-04-26 22:36:03.703 CategoryTest[14271:11303] nsError1 description Error Domain=DOMAIN Code=1 "The operation couldn’t be completed. (DOMAIN error 1.)"
2013-04-26 22:37:30.459 CategoryTest[14271:11303] -[__NSCFString localizedDescription]: unrecognized selector sent to instance 0x901cca0
最佳答案
这是您代码中的错误,而不是Apple中的错误。记录NSUnderlyingErrorKey
具有一个NSError
对象作为与其对应的值。您的代码应改为执行以下操作:
NSError *underlying = [NSError errorWithDomain:NSPOSIXErrorDomain code:1 userInfo:nil];