2012-05-04 94 views
0

我正在編寫一個應用程序,將plist複製到docsdir中,然後將其讀入可變數組中。但是,下面的代碼返回數組的計數爲0。然而,字典日誌的行會返回正確的項目。我還確認該文件正被複制到docsdir。arrayWithFile返回計數爲0

-(NSString *)docsDir { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *listPath = [[self docsDir]stringByAppendingPathComponent:@"list.plist"]; 
    if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) { 
     [[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"list" ofType:@"plist"] toPath:listPath error:nil]; 
     NSLog(@"Chicken"); 
    } 

    NSLog(@"%@", [NSDictionary dictionaryWithContentsOfFile:listPath]); 
    _array = [NSArray arrayWithContentsOfFile:listPath]; 

    NSLog(@"Count: %i", [_array count]); 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

回答

1

通常這是因爲在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>foo</key> 
    <array> 
     <string>foo1</string> 
     <string>foo2</string> 
     <string>foo3</string> 
    </array> 
</dict> 
</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"> 
<array> 
    <string>foo1</string> 
    <string>foo2</string> 
    <string>foo3</string> 
</array> 
</plist> 

其中根元素是一個數組。您現在可以照常編輯。

0

用Xcode打開plist文件。

只需找到位於左上角的'鑰匙'和Type的plist。

Sample Plist file

確保Type是數組。

0

我認爲你的plist是一個包含數組的字典。
試試這個

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:listPath] 
NSArray *array = [dict objectForKey:@"key"]; 
0

的Plist通常被保存在一個字典:

下面是一個例子:

<dict> 
    <key>tiltingAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.25</real> 
     <key>animationFrames</key> 
     <string>1,2,3,4,5,5,4,3,2,1,2,3,4,5</string> 
    </dict> 
    <key>takingAHitAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.1</real> 
     <key>animationFrames</key> 
     <string>5,5,7,8,8</string> 
    </dict> 
    <key>blowingUpAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
    <real>0.2</real> 
    <key>animationFrames</key> 
    <string>5,6,7,8,9,10,11,12,13,14,15,16,17</string> 
    </dict> 
    <key>transmittingAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.3</real> 
     <key>animationFrames</key> 
     <string>5,6,5,6,5,6,5</string> 
    </dict> 
</dict>  

現在,有2個解決方案,以你的問題。

  1. 要麼獲取內容到一個字典,然後拿出一個特定的鍵陣列。
  2. 用文本編輯器打開plist並更改根鍵,然後將root更改爲。如果你的plist是靜態的,並且在bundle資源中,那麼你可以這樣做,但如果它是由你的代碼生成的plist,那麼我不會推薦這個。