Question

I am trying to create a NSError category to make pre-defined errors based upon status codes. However, any time that I try to create a NSError from my category, I get a

+[NSError errorWithDomain:code:userInfo:]: unrecognized selector sent to class 0x2b2b20

I have also tried calling the instance version of the NSError Initializer but this has the same result.

+(NSError*)errorOfType:(ErrorType)errorType inClassNamed:(NSString*)_nsClassName andMessage:(NSString*)_nsMessage
{
    return [[[self class] alloc]initWithType:errorType inClassNamed: _nsClassName andMessage:_nsMessage];
}


#pragma mark -
#pragma mark - Init

-(id)initWithType:(ErrorType)errorType inClassNamed:(NSString*)_nsClassName andMessage:(NSString*)_nsMessage
{
    NSInteger _nCode = [self codeFromErrorType:errorType];
    NSDictionary *_nsUserInfo = [self localizedDescriptionForErrorType:errorType andMessage:_nsMessage];

    NSLog(@"Why Does this fail? Is this Nil? %@, \n%i, \n%@", _nsClassName, _nCode, _nsUserInfo);

    if ([self respondsToSelector:@selector(initWithDomain:code:userInfo:)]) {
        if (self = [self initWithDomain:_nsClassName code:_nCode userInfo:_nsUserInfo]){

        }
    }else{
        NSLog(@"NSERROR Did not create!!!");
    }

    return self;
}

When I run this code calling the static method, The output is:

2013-05-10 17:46:18.851 BMPassSDK-DemoApp[99692:c07] Why Does this fail? Is this Nil? Domain, 
107, 
{
    NSLocalizedDescription = "A connection to the server could not be made. Please Try again later. ";
}
2013-05-10 17:46:18.851 BMPassSDK-DemoApp[99692:c07] NSERROR Did not create!!!
2013-05-10 17:46:18.852 BMPassSDK-DemoApp[99692:c07] +[NSError errorWithDomain:code:userInfo:]: unrecognized selector sent to class 0x2b2b20
(lldb) po 0x2b2b20
$0 = 2829088 NSError
(lldb) 

Anyone have any suggestions on what I am missing?

Was it helpful?

Solution

When I first made this class it was a subclass of NSError. Later, when I tried to convert it from a subclass to category, I accidentally renamed my header file's base class to type NSObject instead of NSError. This is of course what was causing my issue because NSObjects do not respond to the selector initWithDomain:code:userInfo.

Anyways, thanks everyone for looking. Hope it helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top