Why is my app crashing when I allocate NSError userInfo to set a NSDictionary?
-
29-05-2021 - |
Question
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Please, other format selected.", NSLocalizedDescriptionKey, @"This format is not available.", NSLocalizedFailureReasonErrorKey, @"The operation couldn't be completed.", NSUnderlyingErrorKey, self.URL, NSURLErrorKey, nil];
NSLog(@"dict:%@", dict); // no Crash
NSError *error = [NSError errorWithDomain:@"testError" code:1 userInfo:dict];// crash
why crashing when allocate error object?
Here are the crash details:
+[__NSCFConstantString objectForKey:]: unrecognized selector sent to class 0x3ebf5550
Solution
IIRC NSUnderlyingErrorKey
is supposed to be another instance of NSError
.
From the docs:
The user info dictionary can sometimes include another
NSError
object that represents an error in a subsystem underlying the error represented by the containingNSError
. You can query this underlying error object to obtain more specific information about the cause of the error.You access the underlying error object by using the
NSUnderlyingErrorKey
dictionary key.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow