2012-02-01 2 views
0

NSArray 내부에 NSDictionaries가 몇 개 있습니다. 전체 배열에 해당 변경 사항 (테이블 뷰로드)이 있도록 사전 중 하나의 내부에있는 BOOL을 수정하고 싶습니다.사전의 키 업데이트

이 코드가 있지만 BOOL은 변경되지 않습니다. 내가 일할 수있는 mutableCopy 제거해야 같은 느낌,하지만 그때는 정확히, 사본을 수 있습니다 개체의 변경 가능한 사본을 작성하는 경우 그것은

NSLog(@"before: %@", self.currentScopeArray); 
    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:YES] forKey:@"enabledByDefault"]; 
    } else {  
     if (!(self.currentScopeArray.count < 2)) { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      [[[self.currentScopeArray objectAtIndex:iIndexPath.row]mutableCopy] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"]; 
     } 
    } 
    NSLog(@"after: %@", self.currentScopeArray); 

답변

1

당신이있는 NSArray에있는 NSDictionary의 무리를 추가합니다. 난 당신이

  • NSMutableDictionary

왜에있는 NSDictionary를 확인있는 NSMutableArray

  • 에있는 NSArray를 확인하는 것이 좋습니다까요? 이미 사전에서 값을 수정해야하기 때문입니다. 그리고 배열에서 사전을 추가/제거 할 수 있습니다. 당신이 그들을 변경할 만든 후에는 다음이 작업을 수행 할 수 있습니다

    [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:YES] forKey:@"enabledByDefault"]; 
    

    와 현재의 구현과

    [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"]; 
    

    또 다른 문제는 당신이 변경 가능한 사본하지에서 작동한다는 것이다 원래 사전에. 사본이 수정되고 원본은 수정되지 않습니다.

  • 1

    ... 충돌합니다. 이미 currentScopeArray 안에있는 객체를 돌연변이 시키려면 배열에 변경 가능한 객체가 있어야하거나 변경 가능한 사본을 가져와 변형시킨 다음 원래 객체를 바꿔야합니다.

    전 정직하다고 제안합니다. 그래서 당신은 만들 것이다 당신의 currentScopeArray 그것에 NSMutableDictionary의를 추가하고 그냥이 라인을 교체하여이와

    [[[self.currentScopeArray objectAtIndex:iIndexPath.row]mutableCopy] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"]; 
    

    :

    [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"]; 
    
    +0

    그래서'mutableCopy'를 그 줄에서 삭제했는데 그 외에 무엇을해야합니까? 내가하는 일이면 앱이 다운된다. – Jon

    +0

    배열에'NSMutableDictionary'가 있어야 사전이 필요합니다. 어떻게 배열을 처음부터 만들고 있습니까? – mattjgalloway