Читать файл PList - PullRequest
       3

Читать файл PList

0 голосов
/ 05 июня 2011

У меня есть файл 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>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>

Я пытаюсь прочитать этот файл, но я не понимаю, где находится ошибка:

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];

1 Ответ

1 голос
/ 05 июня 2011

В корневом словаре ключ 1 указывает на словарь. Вы пытаетесь присвоить его result, который является NSMutableArray переменной. Итак, вы получаете ошибку. Кроме того, вы создаете выделение экземпляра для result и затем переназначаете его. Это утечка памяти. Массив, который вы будете получать из списка, будет неизменным. Вам придется создать изменяемую копию, если вы собираетесь внести в нее изменения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...