2016-10-12 3 views
0

NSMutableDictionary에 특정 keyvalue이 있습니다. 이 사전은 NSMutableArray 안에 있습니다. 배열의 특정 인덱스에있는 특정 키로 사전을 업데이트하려고합니다. 이 사전 객체의 색인을 찾고 싶습니다. 현재 내 사전은 인덱스 0이고 내 editingIndexPath은 1입니다. NSIndexPath 유형이므로 editingIndexPath.row은 도움이되지 않습니다.NSMutableArray 내 NSMutableDictionary 인덱스 찾기

//UpdatedDateAndTime is the NSMutableArray. 
    //selectedDay is a NSString object. 
    //dateKeySelected is also a string key. 

    [[updatedDateAndTime objectAtIndex:editingIndexPath.row] setObject:selectedDay forKey:dateKeySelected]; 

내 문제는 내가 발견되는 사전의 오른쪽 인덱스를 얻고 싶은 것입니다 다음과 같이

내 코드입니다.

+0

위 코드의 문제점은 무엇입니까? 그것은 열쇠를 설정합니까? 사전의 인덱스가 editingIndexPath.row와 같지 않습니까? –

+0

@TejaNandamuri ... 아니요! 사전이 배열에 이미 있으면 어떻게 될까요? 나를 위해, 그것은 값이 '0'인'index'에 있었고 반면에 tableview에서이 사전에 해당하는 셀은'editingIndexPath.row'가'1'입니다. 사전에 해당하는 배열 인덱스를 원합니다. – Ron

+0

이것이 인덱스 1에있는 것을 어떻게 알 수 있습니까? 사전을 어떻게 비교합니까? – Larme

답변

1

카운터와 루프를로 사용 대답을하지만, 당신은 행운에있어이 :

NSUInteger i = [myArray indexOfObject:@{myKey:myValue}]; 

스위프트 : index(of:)

있는 NSArray는 잘 트릭을 할해야하는 새로운 방법, indexOfObject:을 가지고
+0

인덱스 값이 오래 걸립니다. '9223372036854775807' – Ron

+0

감사합니다. 나는 올바른 색인을 얻을 수있었습니다. 올바른 사전 개체를 전달하지 않아서 왜이 인덱스를 얻었습니까? – Ron

0

배열에 NSMutableDictionary 특수 문자가 하나만있는 경우 아래 코드를 사용하십시오. 테스트하지는 않았지만 배열에서 NSMutableDictionary 클래스를 검색하는 것이 좋습니다. 그리고 당신은 indexPath에서이 데이터를 할당하고자하는 셀과 동일한 검색을해야합니다.

@interface ViewController(){ 
    NSMutableArray *array; 
    NSMutableDictionary *dictionary; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

} 

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (indexPath.row == 1) { 
     for (int i = 0; i < array.count; i++) { 
      if ([updatedDateAndTime[i] objectForKey:@"open_house_updated_date"] == selectedDay) { 
      NSLog(@"%i th index contains NSMutableDictionary that you want",i); 
      //do whatever needed 
      break; 
     } 
     } 
    } 
    else{ 
     //define other cells which doesn't contain NSMutableDictionary 
    } 
    return cell; 
} 

@end 

희망하시는 바입니다.

+0

지원을 확대 해 주셔서 감사합니다. 그러나 배열은 많은 NSMutableDictionary 개체로 구성됩니다. 그러므로, 이것은 사실이 아닙니다. – Ron

+0

올바른 사전을 확인하기위한 기준이 있어야하며 그 이유는 무엇입니까? – icould

+0

NSString * dateKeySelected = @ "open_house_updated_date"; ' 'NSPredicate * predicateString = [NSPredicate predicateWithFormat : @ "% K! = NULL", dateKeySelected]; key' 갖는 // 사전 '있는 NSArray * filteredArray = updatedDateAndTime filteredArrayUsingPredicate : predicateString] ' 'NSMutableDictionary * updatedDay = [NSMutableDictionary의 ALLOC] INIT]' '[updatedDay의 setObject : selectedDay forKey "open_house_updated_date"@ ]; ' – Ron