0

我创建了一个 plist 文件,其中包含一个包含另一个数组的单个数组。每个数组包含四个字典。我已经尝试了很多从 plist 文件中读取数据。但无法获取数据。我的 plist 文件的代码在这里

<plist version="1.0">
<dict>
<key>Root</key>
<array>
    <array>
        <dict>
            <key>name</key>
            <string>Czech Republic</string>
            <key>imagename</key>
            <string>czechrepublic.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Greece</string>
            <key>imagename</key>
            <string>greece.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Poland</string>
            <key>imagename</key>
            <string>poland.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Russia</string>
            <key>imagename</key>
            <string>russia.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
    </array>
    <array>
        <dict>
            <key>name</key>
            <string>Denmark</string>
            <key>imagename</key>
            <string>denmark.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Germany</string>
            <key>imagename</key>
            <string>germany.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Netherlands</string>
            <key>imagename</key>
            <string>netherlands.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Portugal</string>
            <key>imagename</key>
            <string>portugal.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
    </array>
    <array>
        <dict>
            <key>name</key>
            <string>Croatia</string>
            <key>imagename</key>
            <string>croatia.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Italy</string>
            <key>imagename</key>
            <string>italy.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Republic of Ireland</string>
            <key>imagename</key>
            <string>ireland.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Spain</string>
            <key>imagename</key>
            <string>spain.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
    </array>
    <array>
        <dict>
            <key>name</key>
            <string>England</string>
            <key>imagename</key>
            <string>england.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>France</string>
            <key>imagename</key>
            <string>france.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Sweden</string>
            <key>imagename</key>
            <string>sweden.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Ukraine</string>
            <key>imagename</key>
            <string>ukraine.png</string>
            <key>url</key>
            <string>www.google.com</string>
        </dict>
    </array>
    </array>
 </dict>
</plist>

并且用于从此 plist 中读取的代码是

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
{
    // if not in documents, get property list from main bundle
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}

NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithContentsOfFile:plistPath;
NSMutableArray *array = [dict objectForKey:@"Root"];

数组为空。

4

3 回答 3

2

您的 plist 包含一个字典,其中包含一个数组,该数组包含四个数组,而四个数组包含字典。

<plist version="1.0">
<dict>
<key>Root</key>

这些行指定 plist 是字典而不是数组。


已编辑

NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithContentsOfFile:plistPath;
NSMutableArray *array = [temp objectForKey:@"Root"];

你为什么在temp这里使用dict

于 2012-05-15T08:52:29.460 回答
1

您缺少关闭</dict></plist>标签。

于 2012-05-15T08:58:07.663 回答
1

你有没有试过这样编码:

NSString *path = [[NSBundle mainBundle] pathForResource:@”your_Plist_name” ofType:@”plist”];
NSMutableDictionary *saved = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
NSArray *arr = [saved objectForKey:@"root"];

for(int i =0;i<4;i++)
{
  NSArray *next = [arr objectAtIndex:i];
    for(int j=0;j<4;j++)
    {
        NSDictionary *dict = [next objectAtIndex:j];

        NSString *value = [dict objectForKey:@"imagename"];
    }
}

试试这个代码并检查它是否适合你..

于 2012-05-15T09:48:54.457 回答