Search code examples
iosobjective-cobjective-c-category

NSURLSessionTask category method crashes with "Unrecognized selector sent to instance"


I need to add method to NSURLSessionTask. Here's my category which is supposed to do that:

//  NSURLSessionTask+Extras.h

#import <Foundation/Foundation.h>

@interface NSURLSessionTask (Extras)

- (void)helloNSURLSessionTask;

@end

//  NSURLSessionTask+Extras.m

#import "NSURLSessionTask+Extras.h"

@implementation NSURLSessionTask (Extras)

- (void)helloNSURLSessionTask {
    NSLog(@"hello NSURLSessionTask!");
}

@end

Everything compiles well, autocomplete works, but when I call this method, my application crashes:

2014-06-27 12:32:23.916 Test[4333:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFLocalDataTask helloNSURLSessionTask]: unrecognized selector sent to instance 0x109723310'

Same approach works if I add category to NSObject, and I can't seem to understand why it doesn't for NSURLSessionTask.

Here's a test project which reproduces this problem for me: https://dl.dropboxusercontent.com/u/25100182/Test.zip


Solution

  • Unfortunately it doesn't seem that NSURLSession can support categories. I've run into a problem like that trying to add an associated object.
    See also here.