2013-01-23 52 views
0

我无法从下面的plist中获得8个项目。 我有一个plist“settings.plist”。现在我不想从vragen - >类别(self.catagorie - 1) - >问题。这8个项目。但数组保持空白。从plist中读取多个数组

我在做什么错?

代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"instellingen.plist"]; 
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 

NSArray *vragen = [instellingen objectForKey:@"vragen"]; 

NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)]; 
NSArray *question = [cata objectAtIndex: 0]; 
NSLog(@"count: %i", [question count]); 

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>vragen</key> 
    <array> 
     <array> <--catagorydata 
      <array> <-- questiondata 
       <string>vraag</string> 
       <string>A</string> 
       <string>B</string> 
       <string>C</string> 
       <string>D</string> 
       <string>1</string> 
       <string>standaard</string> 
       <string></string> 
      </array> 
      <array> 
       <string>vraag2</string> 
       <string>A</string> 
       <string>B</string> 
       <string>C</string> 
       <string>D</string> 
       <string>1</string> 
       <string>afbeelding</string> 
       <string>afbeelding.jpg</string> 
      </array> 
     </array> 
    </array> 
    <key>firstBoot</key> 
    <true/> 
    <key>regelAtRuntime</key> 
    <true/> 
</dict> 
</plist> 
+0

是可变字典,你得到零(instellingen ==零)? – Alex

+0

当我计算根我得到2作为结果。当我想深入时,结果为0。 –

+1

不应''[vragen objectAtIndex:self.catagorie - 1]'和'[cata objectAtIndex:0]'交换? (以及必要的修改,当然......) – 2013-01-23 16:08:43

回答

2

所有这条线

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"instellingen.plist"]; ? 

首先应该是

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"settings.plist"]; 

我也不得不从字典中删除这些:

<--catagorydata 
<-- questiondata 

除了上述的两件事情,你的代码或你的字典没有错。
进行这些更改后,我测试你的代码,我得到的输出是:

tester[69637:c07] size of dict 3 
tester[69637:c07] count: 8 

根据的.plist文件

这是正确的所以你的问题是在第一次4行。确保您的文件是文件目录(使用类似下面的代码,看看怎么回事错)

NSLog(@"Path : %@",path); 

我复制项目文件夹中的文件(因为我用的模拟器),并用下面的代码,一切工作很好

NSBundle* b=[NSBundle mainBundle]; 
NSURL* path1=[b URLForResource:@"settings" withExtension:@"plist"]; 
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfURL:path1]; 

NSLog(@"size of dict %d",[instellingen count]); 

NSArray *vragen = [instellingen objectForKey:@"vragen"]; 

NSArray *cata = [vragen objectAtIndex: 0]; 
NSArray *question = [cata objectAtIndex: 0]; 
NSLog(@"count: %i", [question count]); 
+0

谢谢,主要问题是文档文件夹中的plist过时了。我必须删除应用程序并重新安装它。 –

0

只是一个快速的读取,但看起来你有 “categoryData” 项的数组,它包含的questionData项的数组; questionData项是字符串。

NSArray *vragen = [instellingen objectForKey:@"vragen"]; // count = 1 
NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)]; // count = 2 
NSString *question = [cata objectAtIndex: 0]; 

建议您添加一些断点并查看您的数据。 OR,NSLog(@“data =%@”,cata);等