2011-01-11 6 views
0

저는 iPhone 프로그래밍에 익숙하지 않고 모든 것을 정렬하려고하기 때문에 다음 형식으로 사전을 plist에 저장하려고합니다. .plist 파일에 여러 사전 저장 및 검색

-Root 
    -StateName (Dictionary) 
     - TitleOfLocation (Dictionary) 
      -Address (String) 
      -City (String) 
      -State (String) 

은 어쩌면 내가 완전히 꺼 여기있어 나 논리를 변경해야했지만 결국 나는 그것이 국가의 이름으로 정렬 jQuery과에 채울 필요가있다.

현재 데이터를 하나의 사전에 저장하고 PLST에 쓸 수 있으며 UITableView로 가져올 수 있습니다 (기본 사항은 예, 알고 있습니다).하지만 말했듯이 중첩 된 사전을 저장하는 데 문제가 있습니다.

누군가 나를 올바른 방향으로 안내 할 수 있습니까? 감사.

+0

저는 사전에 코드를 중첩 할 수 있다고 확신합니다. 즉. '[stateNameDictionary setObject : LocationDictionary forKey : @ "TitleOfLocation"]; ' – mackross

답변

0

중첩 된 사전 대신 중첩 된 배열을 만들 수 있습니다. 그리고 상태의 이름을 보유하는 다른 배열을 만듭니다.

- Root (array) 
- Texas (array) 
    - Location1 (dictionary) 
    - TitleOfLocation (string) 
    - Address (string) 
    - ... 
- Florida (array) 
    - Location1 (dictionary) 
    - ... 
    - Location2 (dictionary) 
    - ... 
// and 
- StateNames (array) 
- Texas (string) 
- Florida (string) 

편의상이 두 배열을 단일 NSDictionary에 넣을 수 있습니다. 나는 나의 예에서 그렇게하지 않았다.
그리고있는 NSArray, NSDictionary에와있는 NSString 모두가 NSCoding 프로토콜 확인하기 때문에 당신은 writeToFile와 디스크에이 사전 쓸 수 있습니다 : 나는이 솔루션은 매우 간단합니다 생각하기 때문에,

내가 당신을 위해 몇 가지 예제 코드를 작성하지만, 하드 : 원자를 단어로 설명하기.

#define kKeyDescription @"kKeyDescription" 
#define kKeyAddress  @"kKeyAddress" 
#define kKeyCity  @"kKeyCity" 
#define kKeyState  @"kKeyState" 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSMutableArray *root = [NSMutableArray array]; 
    NSMutableArray *sectionTitle = [NSMutableArray array]; 

    NSMutableArray *florida = [NSMutableArray array]; 
    [root addObject:florida]; 
    [sectionTitle addObject:@"Florida"]; 

    NSMutableArray *texas = [NSMutableArray array]; 
    [root addObject:texas]; 
    [sectionTitle addObject:@"Texas"]; 

    NSDictionary *location1 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"My favorite pizza place", kKeyDescription, 
           @"5106 Grace Drive", kKeyAddress, 
           @"Miami", kKeyCity, 
           @"Florida", kKeyState, 
           nil]; 
    [florida addObject:location1]; 

    NSDictionary *location2 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"Home", kKeyDescription, 
           @"1234 Foobar Street", kKeyAddress, 
           @"Fort Lauderdale", kKeyCity, 
           @"Florida", kKeyState, 
           nil]; 
    [florida addObject:location2]; 

    NSDictionary *location3 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"Franks Workplace", kKeyDescription, 
           @"9101 Baz Avenue", kKeyAddress, 
           @"Houston", kKeyCity, 
           @"Texas", kKeyState, 
           nil]; 
    [texas addObject:location3]; 

    data = [root retain]; 
    sectionData = [sectionTitle retain]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.data count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [self.sectionData objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[self.data objectAtIndex:section] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellID = @"MyCellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease]; 
    } 
    NSDictionary *object = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    //      < This is the array for the selected state > 
    cell.textLabel.text = [object valueForKey:kKeyDescription]; 
    cell.detailTextLabel.text = [object valueForKey:kKeyAddress]; 
    return cell; 
} 
+0

fluchtpunkt .... 설명과 샘플을 보내 주셔서 감사합니다. 이제 더 많은 의미가 있습니다 ... 나는이 tonite에서 작업 할 것입니다. 내가 함께 할 수있는 것. – Tate