2012-05-04 90 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,那么我不会推荐这个。