2013-02-12 44 views
0

我的時候我的手機上運行的應用程序我得到這個錯誤:- [__ NSCFConstantString allKeys]:無法識別的選擇發送到實例0xf9ac8

我只是試圖解析JSON,並試圖在自定義的兩個列使用的圖片單元格,但滾動中的圖像錯位。

int index=indexPath.row*2; 
int newindex=index+1; 

NSDictionary *u = [[results objectAtIndex:index]mutableCopy ];  
NSLog(@"NSDictionary *u = [results objectAtIndex:index] %@",u); 

NSDictionary *u1 = [[results objectAtIndex:newindex]mutableCopy]; 
NSLog(@"NSDictionary *u1 = [results objectAtIndex:newindex] %@",u1); 

但是,如果我使用索引和newindex相同的價值它的作品。

: - Output i want to display

: - my custom Resultcell

代碼:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil suffix:(NSString *)_suffix{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.suffix = _suffix; 
    } 
    return self; 
} 

#pragma mark Table view methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)_tableView{ 
    return 1; 
} 


//my new 
- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section{ 
    int resultCount = [results count]; 
    labelResultsCount.text = [NSString stringWithFormat:@"%d",resultCount]; 
    return resultCount/2; 
} 
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    // NSString* CellIdentifier = [NSString stringWithFormat:@"ident_%d",indexPath.row]; 

    static NSString *CellIdentifier = @"ResultCell"; 
    ResultCell *cell = (ResultCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil){ 
     NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"ResultCell" owner:self options:nil]; 
     cell = (ResultCell *)[a objectAtIndex:0]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    } 

    int index=[(indexPath.row*2) copy]; 
    int newindex=[(index+1) copy]; 


    // save badge in dictionaory and get here and show in lable 

    NSDictionary *u = [[results objectAtIndex:index]mutableCopy ]; 

    NSLog(@"NSDictionary *u = [results objectAtIndex:newindex] %@",u); 



    NSDictionary *u1 = [[results objectAtIndex:newindex]mutableCopy]; 

    NSLog(@"NSDictionary *u1 = [results objectAtIndex:indexPath.row*2+1 newindex] %@",u1); 


    @try { 
     /* fullName */ 
     NSString *nickName = [u objectForKey:@"nickName"]; 
     cell.labelName.text = nickName; 


     NSString *nickName1 = [u1 objectForKey:@"nickName"]; 
     cell.labelName1.text = nickName1; 

     /* chat notification*/ 

     cell.imageViewNotification.hidden=0; 
     cell.imageViewNotification1.hidden=0; 

     NSString *badge = [u objectForKey:@"badge"]; 
     NSString *badge1 = [u1 objectForKey:@"badge"]; 



     if([badge intValue]>0) 
     { 
      cell.imageViewNotification.hidden=0; 
      cell.labelNotification.text = badge; 
      NSLog(@"inside > 0 %@",badge); 


     } 
     else if([badge intValue]<=0) 
     { 
      cell.imageViewNotification.hidden=1; 
      [email protected]""; 
      NSLog(@"%@",badge); 



     } 



     if([badge1 intValue]>0) 
     { 
      cell.imageViewNotification1.hidden=0; 
      cell.labelNotification1.text = badge1; 
      NSLog(@"inside > 0 %@",badge1); 


     } 
     else if([badge1 intValue]<=0) 
     { 
      cell.imageViewNotification1.hidden=1; 
      [email protected]""; 
      NSLog(@"%@",badge1); 



     } 


     /*..................*/ 


     /* distance */ 
     id distance = [u objectForKey:@"distance"]; 
     if([distance isKindOfClass:[NSString class]]){ 
      cell.labelDistance.text = distance; 
      cell.imageViewDistance.hidden = 0; 
     }else{ 
      cell.imageViewDistance.hidden = 1; 
      cell.labelDistance.text = @""; 
     } 


     id distance1 = [u1 objectForKey:@"distance"]; 
     if([distance1 isKindOfClass:[NSString class]]){ 
      cell.labelDistance1.text = distance1; 
      cell.imageViewDistance1.hidden = 0; 
     }else{ 
      cell.imageViewDistance1.hidden = 1; 
      cell.labelDistance1.text = @""; 
     } 
     /* online */ 
     NSNumber *online = [u objectForKey:@"online"]; 
     cell.imageViewOnline.image = [online intValue] ? [UIImage imageNamed:@"circle_online.png"] : [UIImage imageNamed:@"circle_offline_red.png"]; 


     NSNumber *online1 = [u1 objectForKey:@"online"]; 
     cell.imageViewOnline1.image = [online1 intValue] ? [UIImage imageNamed:@"circle_online.png"] : [UIImage imageNamed:@"circle_offline_red.png"]; 

     /* buttonProfile */ 

     id d = [u objectForKey:@"thumbnails"]; 
     id d1 = [u1 objectForKey:@"thumbnails"]; 

     if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){ 
      if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){ 
       NSString *imageSuffix = [d objectForKey:@"icon"]; 
       NSString *imageSuffix1 = [d1 objectForKey:@"icon"]; 
       NSLog(@"[d allKeys] count]%@", d); 
       NSLog(@"[d1 allKeys] count]%@", d1); 

       UIImage *image = [[SharingCenter sharedManager] imageFromCache:imageSuffix]; 
       UIImage *image1 = [[SharingCenter sharedManager] imageFromCache:imageSuffix1]; 
       if(image||image1) 
       { 
        [cell.buttonUserProfile setBackgroundImage:image forState:UIControlStateNormal]; 
        [cell.buttonUserProfile1 setBackgroundImage:image1 forState:UIControlStateNormal]; 
       } 
       else{ 
        NSString *gender = [u objectForKey:@"gender"]; 
        NSString *gender1 = [u1 objectForKey:@"gender"]; 

        UIImage *profileDefualtImage = [gender isEqualToString:@"M"] ? [UIImage imageNamed:@"no_photo_male.png"] : [UIImage imageNamed:@"no_photo_female.png"]; 

        UIImage *profileDefualtImage1 = [gender1 isEqualToString:@"M"] ? [UIImage imageNamed:@"no_photo_male.png"] : [UIImage imageNamed:@"no_photo_female.png"]; 


        [cell.buttonUserProfile setBackgroundImage:profileDefualtImage forState:UIControlStateNormal]; 
        [cell.buttonUserProfile1 setBackgroundImage:profileDefualtImage1 forState:UIControlStateNormal]; 



        /* downlowd Image */ 
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",WebServicePrefix,imageSuffix]]; 
        __block 
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
        request.delegate = self; 
        [request setPostValue:[NSString stringWithFormat:@"%f",[[SharingCenter sharedManager] currentCoordinate].latitude] forKey:@"lat"]; 
        [request setPostValue:[NSString stringWithFormat:@"%f",[[SharingCenter sharedManager] currentCoordinate].longitude] forKey:@"lon"]; 
        // [[[SharingCenter sharedManager] imagesCache] removeAllObjects]; 

        [request setCompletionBlock:^{ 
         NSData *imageData = [request responseData]; 
         if(imageData){ 
          UIImage *image = [UIImage imageWithData:imageData]; 
          if(image){ 


           [[[SharingCenter sharedManager] imagesCache] setObject:image forKey:imageSuffix]; 

           if([[self.tableView indexPathsForVisibleRows] containsObject:indexPath]){ 
            ResultCell *cell = (ResultCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
            [cell.buttonUserProfile setBackgroundImage:image forState:UIControlStateNormal]; 
           } 
          } 
         } 
        }]; 
        [request setFailedBlock:^{ 
         NSError *error = request.error; 
         NSLog(@"%@",error); 
        }]; 

      /* downlowd Image */ 
      NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",WebServicePrefix,imageSuffix1]]; 
      __block 
      ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1]; 
      request1.delegate = self; 
      [request1 setPostValue:[NSString stringWithFormat:@"%f",[[SharingCenter sharedManager] currentCoordinate].latitude] forKey:@"lat"]; 
      [request1 setPostValue:[NSString stringWithFormat:@"%f",[[SharingCenter sharedManager] currentCoordinate].longitude] forKey:@"lon"]; 
      // [[[SharingCenter sharedManager] imagesCache] removeAllObjects]; 

      [request1 setCompletionBlock:^{ 
       NSData *imageData1 = [request1 responseData]; 
       if(imageData1){ 
        UIImage *image1 = [UIImage imageWithData:imageData1]; 
        if(image1){ 

         [[[SharingCenter sharedManager] imagesCache] setObject:image1 forKey:imageSuffix1]; 

         if([[self.tableView indexPathsForVisibleRows] containsObject:indexPath]){ 
          ResultCell *cell = (ResultCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
          [cell.buttonUserProfile1 setBackgroundImage:image1 forState:UIControlStateNormal]; 
         } 
        } 
       } 
      }]; 
      [request1 setFailedBlock:^{ 
       NSError *error = request1.error; 
       NSLog(@"%@",error); 
      }]; 
        [[self networkQueue] addOperation:request]; 
        [[self networkQueue] addOperation:request1]; 




     } 
    } 
    } 


    else{ 
      NSString *gender = [u objectForKey:@"gender"]; 
     NSString *gender1 = [u1 objectForKey:@"gender"]; 

      UIImage *profileDefualtImage = [gender isEqualToString:@"M"] ? [UIImage imageNamed:@"no_photo_male.png"] : [UIImage imageNamed:@"no_photo_female.png"]; 
      [cell.buttonUserProfile setBackgroundImage:profileDefualtImage forState:UIControlStateNormal]; 


     UIImage *profileDefualtImage1 = [gender1 isEqualToString:@"M"] ? [UIImage imageNamed:@"no_photo_male.png"] : [UIImage imageNamed:@"no_photo_female.png"]; 
     [cell.buttonUserProfile1 setBackgroundImage:profileDefualtImage1 forState:UIControlStateNormal]; 
     }  

     /* pickStatus */ 
     NSNumber *pickStatus = [u objectForKey:@"pick_status"]; 
     NSNumber *pickStatus1 = [u1 objectForKey:@"pick_status"]; 

     switch ([pickStatus intValue]) { 
      case 0: 
       cell.imageViewBorder.image = [UIImage imageNamed:@"border_yellow_transperent.png"]; 
       cell.buttonUserPick.enabled = 1; 
       [cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:@"button_circle_pick.png"] forState:UIControlStateNormal]; 


       break; 
      case 1: 
       cell.imageViewBorder.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick.enabled = 0; 
       [cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:@"button_circle_wait.png"] forState:UIControlStateDisabled]; 



       break; 
      case 2: 
       cell.imageViewBorder.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick.enabled = 1; 
       [cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:@"button_circle_pick.png"] forState:UIControlStateNormal]; 
       break; 
      case 3: 
       cell.imageViewBorder.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick.enabled = 0; 
       [cell.buttonUserPick setBackgroundImage:[UIImage imageNamed:@"button_circle_date.png"] forState:UIControlStateDisabled]; 



       break; 
      default: 
       break; 
     } 



     switch ([pickStatus1 intValue]) { 
      case 0: 
       cell.imageViewBorder1.image = [UIImage imageNamed:@"border_yellow_transperent.png"]; 
       cell.buttonUserPick1.enabled = 1; 
       [cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:@"button_circle_pick.png"] forState:UIControlStateNormal]; 


       break; 
      case 1: 
       cell.imageViewBorder1.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick1.enabled = 0; 
       [cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:@"button_circle_wait.png"] forState:UIControlStateDisabled]; 



       break; 
      case 2: 
       cell.imageViewBorder1.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick1.enabled = 1; 
       [cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:@"button_circle_pick.png"] forState:UIControlStateNormal]; 
       break; 
      case 3: 
       cell.imageViewBorder1.image = [UIImage imageNamed:@"border_red_transperent.png"]; 
       cell.buttonUserPick1.enabled = 0; 
       [cell.buttonUserPick1 setBackgroundImage:[UIImage imageNamed:@"button_circle_date.png"] forState:UIControlStateDisabled]; 



       break; 
      default: 
       break; 
     } 
    } 
    @catch (NSException * e) { 
     NSLog(@"%@",e); 
    } 
    @finally { 
     return cell; 
    } 
} 
+0

你是不是在調用allKeys?這個錯誤是說你發送它到一個字符串,當你認爲你將它發送到字典。 – rdelmar 2013-02-12 07:32:36

+0

這些行不包含錯誤。 – Alexander 2013-02-12 07:37:43

+0

您應該設置一個異常斷點並查看它是否將查明導致錯誤的行。 – rdelmar 2013-02-12 07:51:37

回答

1

猜測:

此行導致您的問題:

if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){ 
    if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){ 

有4點不同的可能性如何,第一,如果將出來:

  1. d和d1都是NSString的。第2行不會叫
  2. d是NSDictionary,d1是NSString。第2行將被稱爲
  3. d是NSString,d1是一個NSDictionary。第2行將被稱爲
  4. d和d1都是NSDictionaries。第2行將被調用。

第一種情況完全沒有問題。最後的情況都沒有在情況2和3中,allKeys將在不是NSDictionary的對象上調用。

你應該用一個「if」來代替它,它需要兩個測試都是真的。

if([d isKindOfClass:[NSDictionary class]] && [d1 isKindOfClass:[NSDictionary class]]){ 
              ^^ 

另一種選擇是單獨檢查。

if([d isKindOfClass:[NSDictionary class]]) { 
    x = [d allKeys]; 
} 
if([d1 isKindOfClass:[NSDictionary class]]) { 
    x = [d1 allKeys]; 
} 
1

你可能會做一些這些NSDictionary對象的地方,閱讀他們的allKeys。但我的猜測是,您嘗試從results陣列中獲取的對象不是NSDictionary對象,而是NSString對象。

嘗試記錄他們的class屬性,看看你在處理什麼(你也可以通過查看JSON來找到這個)。

NSDictionary *u = [[results objectAtIndex:index]mutableCopy ]; 
NSLog(@"u Class: %@", [u class]); 

另外,爲什麼你在做對象的mutableCopy權到非可變NSDictionary對象。

使用以下應該做通常的伎倆:

NSDictionary* u = [results objectAtIndex:index]; 
NSLog(@"u Class: %@", [u class]); 
+0

太棒了,只是很高興它解決了! – 2013-02-12 08:09:03

1

這是一個很大的代碼,翻閱,但我覺得這可能是問題:

if([d isKindOfClass:[NSDictionary class]] ||[d1 isKindOfClass:[NSDictionary class]]){ 
      if([[d allKeys] count]>0 ||[[d1 allKeys] count]>0){ 

這個if語句會通過如果d或d1是字典。如果沒有,它會導致你在第二行看到的錯誤。記錄d和d1的類以查看它們是否都是字典。

+0

如果我使用索引和newindex相同的值,它不顯示該錯誤。但我不想讓這個結果。 – iphonemaclover 2013-02-12 07:57:37

相關問題