大佬教程收集整理的这篇文章主要介绍了iOS:在运行时没有找到NSURLSession类别方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
-[__NSCFURLSession doSomething]: unrecognized SELEctor sent to instance 0xbf6b0f0 <unkNown>:0: error: -[NSURLSession_UPnPTests TESTCategory] : -[__NSCFURLSession doSomething]: unrecognized SELEctor sent to instance 0xbf6b0f0
很奇怪,这里是我建立的一个测试类来显示问题. NSString上的类别工作正常,但NSURLSession上的类别在运行时无法找到该方法.我怀疑这是内在的东西.
意见:-)
#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
如果我是你,我会放弃这种方法,并使用组合(即使NSURLSession成为另一个对象中的ivar).如果您需要使用NSURLSession存储一些信息,可以将JSON编码的字典填入.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类别方法全部内容,希望文章能够帮你解决iOS:在运行时没有找到NSURLSession类别方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。