iOS:在运行时找不到NSURLSession类别方法 [英] iOS: NSURLSession category methods not being found at runtime
问题描述
我想在NSURLSession上创建一个类别。该应用程序编译好,但当我尝试调用类别方法时,我得到
I wanted to create a category on NSURLSession. The app compiles ok, but when I attempt to call the category methods I get
-[__NSCFURLSession doSomething]: unrecognized selector sent to instance 0xbf6b0f0
<unknown>:0: error: -[NSURLSession_UPnPTests testCategory] : -[__NSCFURLSession doSomething]: unrecognized selector sent to instance 0xbf6b0f0
非常奇怪,这是我为了显示问题而构建的测试类。 NSString上的类别工作正常,但NSURLSession上的类别无法在运行时找到该方法。我怀疑这是内部的东西。
Very strange, here is a test class I built to show the issue. The category on the NSString works fine, but the category on NSURLSession fails to find the method at runtime. I suspect this is something internal.
意见请: - )
#import <XCTest/XCTest.h>
@interface NSString (test)
-(void) doSomethingHere;
@end
@implementation NSString (test)
-(void) doSomethingHere {
NSLog(@"Hello string");
}
@end
@interface NSURLSession (test)
-(void) doSomething;
@end
@implementation NSURLSession (test)
-(void) doSomething {
NSLog(@"Hello!!!!");
}
@end
@interface NSURLSession_UPnPTests : XCTestCase
@end
@implementation NSURLSession_UPnPTests
-(void) testCategory {
[@"abc" doSomethingHere];
NSURLSession *session = [NSURLSession sharedSession];
[session doSomething];
}
@end
推荐答案
我在backgrounded NSURLSessionUploadTask
中获得了类似的结果,在 __ NSCFURLSessionUploadTask
s > -URLSession:task:didCompleteWithError:委托回调。
I've had similar results with backgrounded NSURLSessionUploadTask
s, which get deserialized as __NSCFURLSessionUploadTask
s during the -URLSession:task:didCompleteWithError:
delegate callback.
如果我是你,我会放弃这种方法,并使用构图(即在另一个对象中使 NSURLSession
成为一个ivar)。如果您需要使用 NSURLSession
存储一些信息,您可以将JSON编码的字典填充到 .sessionDescription
中。
If I were you, I'd give up on that approach, and use composition (i.e. make the NSURLSession
an ivar in another object). If you need to store some info with the NSURLSession
, you could stuff a JSON-encoded dictionary into the .sessionDescription
.
以下是我过去为执行的代码:
#pragma mark - Storing an NSDictionary in NSURLSessionTask.description
// This lets us attach some arbitrary information to a NSURLSessionTask by JSON-encoding
// an NSDictionary, and storing it in the .description field.
//
// Attempts at creating subclasses or categories on NSURLSessionTask have not worked out,
// because the –URLSession:task:didCompleteWithError: callback passes an
// __NSCFURLSessionUploadTask as the task argument. This is the best solution I could
// come up with to store arbitray info with a task.
- (void)storeDictionary:(NSDictionary *)dict inDescriptionOfTask:(NSURLSessionTask *)task
{
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSString *stringRepresentation = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[task setTaskDescription:stringRepresentation];
[stringRepresentation release];
}
- (NSDictionary *)retrieveDictionaryFromDescriptionOfTask:(NSURLSessionTask *)task
{
NSString *desc = [task taskDescription];
if (![desc length]) {
DDLogError(@"No description for %@", task);
return nil;
}
NSData *data = [desc dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = (data ? (id)[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil);
if (!dict) {
DDLogError(@"Could not parse dictionary from task %@, description\n%@", task, desc);
}
return dict;
}
这篇关于iOS:在运行时找不到NSURLSession类别方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!