2013-03-15 3 views
1

Я хочу извлечь значения словаря, но возвращает null. Зачем?objectForKey возвращает null, но nsdictionary заполнен

Мой код:

- (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; 

} 
+0

Вы уверены, что это только словарь nslog? –

+0

вы, кажется, написаны просто не понимают ошибки –

+0

вы можете просто показать мне NSLog из '[словарь AllKeys]' –

ответ

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 
} 
Смежные вопросы