Question

I think I've found a bug on NSError but want to run it passed you guys and see if it's actually just me doing something wrong.

I get a crash when sending description to an NSError instance that makes use of userInfo. If userInfo is set to nil description works as expected.

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

Output:

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
Was it helpful?

Solution

This is a mistake in your code, not a bug in Apple's. NSUnderlyingErrorKey is documented to have an NSError object as the value corresponding to it. Your code should instead do something like:

NSError *underlying = [NSError errorWithDomain:NSPOSIXErrorDomain code:1 userInfo:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top