2011-04-19 6 views
0

plist에 둘 이상의 사전을 추가해야합니다. 어떻게 할 수 있습니까?plist에서 사전에 사전을 추가하는 방법은 무엇입니까?

NSMutableDictionary *rootDict = [[[NSMutableDictionary alloc] init] autorelease]; 
NSMutableDictionary *rowsDict = [[[NSMutableDictionary alloc] init] autorelease]; 
NSMutableDictionary *itemZeroDict1 = [[[NSMutableDictionary alloc] init] autorelease]; 

나는 rootDictrowsDictitemZeroDict1를 추가해야합니다.

+0

.plist로 작성하거나 읽는 데 사용하는 코드를 게시하십시오. 그러면 사람들이 사전을 추가하는 방법을 보여줄 수 있습니다. –

+1

관련 코드를 사용하여 질문을 편집하십시오. 의견을 게시하지 마십시오. – Juliet

+1

앞으로도 [코드를 올바르게 포맷하십시오] (http://stackoverflow.com/editing-help). 감사! –

답변

0
NSDictionary *root = [[NSDictionary alloc] initWithObjectsAndKeys: 
         [NSDictionary dictionaryWithObject:@"1" forKey:@"number"], 
         [NSDictionary dictionaryWithObject:@"2" forKey:@"number"], 
         nil]; 

NSString *error = nil; 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:root 
                   format:NSPropertyListBinaryFormat_v1_0 
                errorDescription:&error]; 
if (plistData == nil) 
    NSLog(@"Error serializing plist: %@", error); 
else 
    // Do whatever you want with plistData 
[error release]; // this is an exception to the usual memory management in Cocoa 
0

.plist 파일에 사전을 쓰려면 아래 코드를 사용하십시오.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, 
                  YES); 
     NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"friendList.plist"]; 

/* Code to remove Existing file from Document directory */ 

     if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){ 
      [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil]; 
     } 
//////////////// 

     /** Copy Plist file from your bundle resource to Document directory */ 

     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"friendList" ofType:@"plist"]; 
     BOOL sucesss = [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:nil]; 
     if (!sucesss) { 

     } 

     NSMutableArray *arryDict = [[NSMutableArray alloc] init]; 

     for (NSDictionary *dict in arryOfDictionary) { 

      NSDictionary *dictFriend = @{@"key":@"value"}; 

      [arryDict addObject:dictFriend]; 
     } 

     [arryDict writeToFile:plistPath atomically:NO]; 
관련 문제