首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取PList文件

读取PList文件
EN

Stack Overflow用户
提问于 2011-06-05 22:51:45
回答 1查看 613关注 0票数 0

我有一个Plist文件,它是这样组成的:

代码语言:javascript
复制
<?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>

我尝试读取此文件,但我不明白错误在哪里:

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2011-06-05 23:03:41

在根字典中,键1指向字典。您正试图将它赋给NSMutableArray变量result。所以你得到的是错误。另外,您将创建一个实例分配给result,然后重新分配它。这是一个内存泄漏。你将从plist得到的数组将是一个不可变的数组。如果您打算对其进行更改,则必须创建一个可变副本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6243637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档