2013-03-15 88 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是唯一的嗎? – 2013-03-15 17:10:37

+0

你似乎已經寫只是不理解的錯誤 – 2013-03-15 17:39:34

+0

你可以告訴我的'[字典allKeys]' – 2013-03-15 17:42:01

回答

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 
} 
相關問題