从类别调用方法在iOS 7.1中给出“无法识别的选择器错误”,但在8.4中它没关系

时间:2015-08-06 11:37:32

标签: ios objective-c objective-c-category

基本上,问题标题解释了一切。我尝试了很多东西,但它不起作用。

我有扩展- (NSURLSessionDataTask *) dataTaskHandlingErrorsWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *))completionHandler { return [self dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (!error && response && [response isKindOfClass:[NSHTTPURLResponse class]]) { NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response; if (resp.statusCode / 100 == 2) { completionHandler(data); } else { // Wrong status code from server. NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:kPANotifiNameServerStatusError object:self userInfo:@{ @"response" : resp }]; NSLog(@"Wrong status code"); completionHandler(nil); } } else { // Something wrong with network. NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:kPANotifiNameNetworkError object:self userInfo:@{ @"error" : error ? error : [NSNull null] }]; NSLog(@"Internet connection problem."); completionHandler(nil); } }]; } 的类别,用于处理服务器和网络错误

- (void)authenticateWithHandler:(AuthHandler)handler {
  NSURLSession *session = [NSURLSession sharedSession];
  NSURLSessionTask *task =
      [session dataTaskHandlingErrorsWithRequest:self.request
                               completionHandler:^(NSData *data) {
                                 if (data) {
                                   BOOL suc = [self handleResponse:data];
                                   handler(suc);
                                 } else {
                                   handler(NO);
                                 }
                               }];
  [task resume];
}

这是我从以下地方打电话的地方:

2015-08-06 15:29:50.973 MyApp[2618:607] -[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40
2015-08-06 15:29:50.976 MyApp[2618:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40'

所以,在iOS 7.1上,如果我正在调用该方法,它会抛出

  $getimages=($con,"select image,id,somethingelse from tablename where id !='' order by rand() limit 21"); 
 // i want 21 images to show here 7*3 (the image here repets it self by three)
 while($theimagelink=mysqli_fetch_array($getimages)){
   // i think array_push may help but dont know how to implement..
 }

但是在iOS 8上它可行。我检查了编译源,一切都很好。

1 个答案:

答案 0 :(得分:1)

您可以签入类别方法:

if ([self isKindOfClass:[NSURLSession class]]) {
    NSLog(@"YES");
}

也许您尝试运行它的对象只是投放到NSURLSession,而事实上它不是......?只是一些猜测,因为我仍然不知道。

我想在这里指出Mattt answer,这有助于理解它是如何运作的。