2011-06-05 89 views
0

我有這樣組成的plist文件:讀取plist文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>1</key> 
    <dict> 
     <key>2</key> 
     <array> 
      <array> 
       <string>a</string> 
       <integer>1</integer> 
      </array> 
      <array> 
       <string>b</string> 
       <integer>0</integer> 
      </array> 
      <array> 
       <string>c</string> 
       <string>0</string> 
      </array> 
     </array> 
    </dict> 
</dict> 
</plist> 

我嘗試讀取這個文件,但我不明白的地方是錯誤:

NSError *error; 

    NSMutableArray *result=[[NSMutableArray alloc] initWithCapacity:1]; 

    NSString *pList = @"a.plist"; // 

    NSString *plistPath; 

    //path 

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    plistPath = [rootPath stringByAppendingPathComponent:pList]; 

    //check if the path exist 

    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    success = [fileManager fileExistsAtPath:plistPath]; 

    if (!success) { 

     NSString *path = [[NSBundle mainBundle] pathForResource:pList ofType:@""]; 

     [fileManager copyItemAtPath:path toPath:plistPath error:&error]; 
     NSLog(@"error"); 
    } 

    //read the dictionary 

    NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 

    //get value for key of the dictionary 



    result=[temp valueForKey:@"1"]; 



    NSMutableArray *tmp = [result objectAtIndex:1]; //this give me error 

    NSLog(@"n %d", [tmp count]); 

    ... 

    if(!temp) [temp release]; 

回答

1

在根字典中,鍵1指向字典。您正試圖將其分配給result這是一個NSMutableArray變量。所以你得到的錯誤。另外,您正在創建將實例分配給result,然後重新分配它。這是內存泄漏。你將從plist得到的數組將是不可變的。如果您打算對其進行更改,則必須創建可變副本。