2012-06-11 2 views
3

하나의보기에서 속성 목록에 데이터를 저장하고 다른보기에서 검색하려고합니다. 나는이 같은 외모를 저장하고있어 데이터의 구조는 :중첩 된 NSDictionary 속성 목록에서 검색

키 (있는 NSString) 값 (NSDictionary와는)

NSDictionary은 키와 값이 모두 문자열을 포함합니다.

NSDictionary *savedRSSItemsDictionary = [NSDictionary dictionaryWithContentsOfFile:[self propertyListFilePath]]; 
NSDictionary *RSSDictionary = [savedRSSItemsDictionary objectForKey:[savedRSSItemsArray objectAtIndex:indexPath.row]]; 

는 첫 번째 라인 루트를 검색 : 나는 같은 것을 할 데이터를 검색하기 위해 노력하고있어 때

- (IBAction)saveRSSItem:(id)sender { 

    NSMutableDictionary *dictionary; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]){ 
     dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath]]; 
    } 
    else { 
     dictionary = [[NSMutableDictionary alloc] init]; 
    } 

    //Create Dictionary to store RSSItem information 
    NSMutableDictionary *RSSdictionary = [[NSMutableDictionary alloc] init]; 

    if (self.selectedItem.title) 
     [RSSdictionary setValue:@"title" forKey:self.selectedItem.title]; 

    if (self.selectedItem.summary) 
     [RSSdictionary setValue:@"summary" forKey:self.selectedItem.summary]; 

    if (self.selectedItem.author) 
     [RSSdictionary setValue:@"author" forKey:self.selectedItem.author]; 

    if (self.selectedItem.date) 
     [RSSdictionary setValue:@"date" forKey:self.selectedItem.date]; 

    if (self.selectedItem.content) 
     [RSSdictionary setValue:@"content" forKey:self.selectedItem.content]; 

    //Add saved RSSItem to dictionary 
    [dictionary setValue:RSSdictionary forKey:self.selectedItem.title]; 

    //Overwrite the updated dictionary in the property list 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:[self dataFilePath]]; 
    [dictionary writeToURL:url atomically:YES]; 
} 

: 버튼을 누를 때마다, 나는 구원을 위해이 방법을 발사 사전. 두 번째 줄은 속성 목록 (NSDictionary)의 첫 번째 행의 값을 검색합니다.

그러나 valueForKey : RSSDictionary에서 메서드를 사용할 수 없습니다. 그게 아니라고 생각합니다 NSDictionary? 어떤 아이디어가 NSDictionary을 속성 목록의 값 필드에 가져올 수없는 이유는 무엇입니까? NSPropertySerialization을 사용해야합니까?

+0

이상한 들여 쓰기에 불쌍합니다. 이것은 내 첫 게시물입니다 :) –

+4

(a) 나는 당신이 값을 혼동스럽게 생각하고 열쇠를 첫 코드 조각에 넣었습니다. (b)'savedRSSItemsArray'와'indexPath.row'에 무엇이 있는지에 대한 단서를 제공하지 않으므로 다른 사람이 여러분의 두 번째 코드 조각을 돕는 것이 어렵습니다. 디버거에서 코드를 실행하고'RSSDictionary'가 어떻게 설정되는지 살펴 보았습니까? 만약 당신이 다른 변수를보고 거꾸로 작업을 기대하지 않습니다. – CRD

+0

내 코드를 살펴 줘서 고마워. 함수 호출에서 값과 키를 착각하고 있습니다. 그게 가장 가능성이 모든 것을 해결할 것입니다 :) –

답변

0
NSDictionary *savedRSSItemsDictionary = [NSDictionary 
      dictionaryWithContentsOfFile:[self propertyListFilePath]]; 
NSDictionary *RSSDictionary = 
    [savedRSSItemsDictionary objectForKey:[savedRSSItemsArray 
             objectAtIndex:indexPath.row]]; 

올바른 키를 지정하지 않았기 때문에 잘못되었습니다.

데이터를 저장하려면 NSArray를 사용해야합니다. 사전에있는 필드의 값으로 사전을 색인하는 것은 나쁜 생각입니다.