objectForKey返回null但nsdictionary已满

时间:2013-03-15 17:07:34

标签: ios nsarray nsdictionary

我想提取字典的值但返回null。为什么呢?

我的代码:

 - (void)viewDidLoad
 {

    //....
  self.mutableArray =[[NSMutableArray alloc]init];
  self.mutableDictionary =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Carrello", @"Name List", @"18", @"Number", nil];
  [mutableArray addObject:self.mutableDictionary];
    //....
  }

 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

       static NSString *CellIdentifier = @"Login";

       self.cellCustom = (CellCustomTableList*)[aTableView 
                 dequeueReusableCellWithIdentifier:CellIdentifier];

      if(self.cellCustom == nil){
              self.cellCustom = [[CellCustomTableList alloc] 
              initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
       }


   NSDictionary * dictionary = [self.mutableArray objectAtIndex:indexPath.row];
   NSLog(@"the dictionary %@", dictionary);
   /*the dictionary {
          "Name List" = Carrello;
          "Number" = 18;
    }*/

   NSLog(@"%@", [dictionary objectForKey:@"Name List"]);
   // (null)
   NSLog(@"%@", [dictionary objectForKey:@"Number"]);
   // (null)
   NSLog(@"%@", [dictionary allKeys]);
   /*(
        "Name List",
        "Number"
    )
    */

    int i =[self.mutableArray indexOfObject:dictionary];
     NSLog(@"%d", i); 
     //0

   return self.cellCustom;

 }

1 个答案:

答案 0 :(得分:0)

如果你的tableView中有更多的行,你的字典中有对象,只需像这样进行简单的检查:

//if you want some specific object
NSUInteger objIdx = [self.mutableArray indexOfObject: someObject];
if(objIdx != NSNotFound) {
    // Do some alter stuff here
}

//or simple checking
if (indexPath.row <= [self.mutableArray count]){
// Do some alter stuff here
}