2010-08-09 6 views
8

NSMutableDictionary에서 동일한 키에 대해 여러 값을 갖는 방법은 tel입니까? 내 경우 최근 한NSMutableDictionary Objective-C 프로그래밍에서 많은 값을 지닌 단일 키 사용

와 내가 아래의 방법을 사용하는 경우, 값이되기 때문에 대체합니다은 :

[dictionary setObject:forename forKey:[NSNumber numberWithint:code]]; 
[dictionary setObject:surname forKey:[NSNumber numberWithint:code]]; 
[dictionary setObject:reminderDate forKey:[NSNumber numberWithint:code]]; 

내가 사전의 내용을 볼

, 난 키 코드에 대해서만 reminderDate를 얻을. 여기 코드는 모든 값에 대해 동일합니다. plansReminder로 대체되는 forename과 성을 피하는 방법을 도와주세요.

는 키로 code을 사용하는 것 같다 당신

답변

15

감사 당신은 code에 따라 여러 값을 표현하고 싶다. 이 경우, 당신은해야 하나 :

  1. 추상 모든 (아마도 Person라는) 별도의 클래스로 code과 관련된 데이터와 사전에 값으로이 클래스의 인스턴스를 사용합니다. 사전

  2. 를 사용하여 하나 개 이상의 레이어 : 나는 당신이 사전의 작동 방식을 이해 생각하지 않는다

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 
    
    NSMutableDictionary *firstOne = [NSMutableDictionary dictionary]; 
    [firstOne setObject:forename forKey:@"forename"]; 
    [firstOne setObject:surname forKey:@"surname"]; 
    [firstOne setObject:reminderDate forKey:@"reminderDate"]; 
    
    [dictionary setObject:firstOne forKey:[NSNumber numberWithInt:code]]; 
    
    // repeat for each entry. 
    
+9

downvote 만하지 말고 나에게 고칠 수 있도록 잘못되었습니다. – dreamlax

1

. 각 키는 하나의 값만 가질 수 있습니다. 사전이나 배열 사전을 원할 것입니다.

여기서 각 사람에 대한 사전을 만든 다음 마스터 사전에 저장합니다. 당신이 사전에 객체를 저장 정말 단호하고, 당신이 문자열을 다루고있는 경우 키에서 개체를 검색 할 때, 당신은 항상 다음 함께 쉼표로 구분하여 문자열을 모두 추가하고, 할 수 있다면

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys: 
forename, @"forename", surname, @"surname", @reminderDate, "@reminderDate", nil]; 

[dictionary setObject:d forKey:[NSNumber numberWithint:code]]; 
5

, 모든 개체를 준 csv 형식으로 갖게됩니다! 그런 다음 해당 문자열을 객체 배열로 쉽게 파싱 할 수 있습니다. 검색하고 사전에 객체를 구문 분석 한 후

NSString *forename = @"forename"; 
NSString *surname = @"surname"; 
NSString *reminderDate = @"10/11/2012"; 
NSString *code = @"code"; 

NSString *dummy = [[NSString alloc] init]; 
dummy = [dummy stringByAppendingString:forename]; 
dummy = [dummy stringByAppendingString:@","]; 
dummy = [dummy stringByAppendingString:surname]; 
dummy = [dummy stringByAppendingString:@","]; 
dummy = [dummy stringByAppendingString:reminderDate]; 
dummy = [dummy stringByAppendingString:@","]; 
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 
[dictionary setObject:dummy forKey:code]; 

을 그리고 : : 그것은 사전의 층을 갖는만큼 깨끗하지 않을 수 있습니다

NSString *fromDictionary = [dictionary objectForKey:code]; 
NSArray *objectArray = [fromDictionary componentsSeparatedByString:@","]; 
NSLog(@"object array: %@",objectArray); 

여기

당신이 실행할 수 있습니다 몇 가지 예제 코드 dreamlax가 제안했듯이, 당신이 키를위한 배열을 저장하고자하는 사전을 다루고 있고 그 배열의 객체들이 특정한 키를 가지고 있지 않다면, 이것은 해결책입니다!

+0

단순히 NSString * dummy = stringWithFormat : @ "% @, % @, % @", forename, surname, reminderDate];를 사용하지 않는 것이 좋을 것입니다. 적어도 그런 식으로 연결하는 경우에는 NSMutableString을 사용해야합니다. – dreamlax

1

현대 구문이 훨씬 깔끔합니다.

당신이로드시 정적 구조를 구축하는 경우

A. : (아마도) 실시간으로 추가하는 항목이있는 경우

NSDictionary* dic = @{code : @{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate}/*, ..more items..*/}; 

B를 :

NSMutableDictionary* mDic = [[NSMutableDictionary alloc] init]; 
[mDic setObject:@{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate} forKey:code]; 
//..repeat 

그런 다음 당신이로 액세스 2D 사전 ...

mDic[code][@"forename"];