开发者

Unable to write to plist file

开发者 https://www.devze.com 2023-04-11 11:34 出处:网络
I\'ve found several questions here on the site with people who have a similar problem, but I\'m still not able to resolve mine. Here\'s the thing:

I've found several questions here on the site with people who have a similar problem, but I'm still not able to resolve mine. Here's the thing:

I'm making an iOS app that contains two plist files. When the user opens the app for the first time, the application takes care of moving these two files to the Documents directory. I know this code works, since I'm subsequently able to read from each of the files in the Documents directory path. However, I'm not able to write to any of them for apparent unknown reasons. Here is the code I use to write to the first plist file:

+ (void)insertNewElementWith:(NSArray *)objects andKeys:(NSArray *)keys {
    NSString *filePath = [self returnPathForFirstPlistFile];
    NSMutableDictionary *contentOfFirstPlistFile = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    [contentOfFirstPlistFile setObject:dictionary forKey:[NSNumber numberWithInt:[contentOfFirstPlistFile count]]];
    [contentOfFirstPlistFile writeToFile:filePath at开发者_Python百科omically:YES];
    [dictionary release];
}

returnPathForFirstPlistFile does work, since contentOfFirstPlistFile contains { } rather than null. Can anyone explain to me, though, why it keeps returning { }, even though the above method has been called several times.

Update

Here is the code I use to place the files in the Documents directory. placePlistsInDocumentsDirectory is only called when the user logs in through the app, so this only happens once.

+ (NSString *)returnPathForFirstPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"First.plist"];

    return filePath;
}

+ (NSString *)returnPathForSecondPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [paths objectAtIndex:0]);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Second.plist"];

    return filePath;
}

+ (void)placePlistsInDocumentsDirectory {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *firstPlistPath = [self returnPathForFirstPlistFile];
    NSString *secondPlistPath = [self returnPathForSecondPlistFile];

    if (![fileManager fileExistsAtPath:firstPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"First" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:firstPlistPath error:&error];
    }

    if (![fileManager fileExistsAtPath:secondPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Second" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:secondPlistPath error:&error];
    }
}


[NSNumber numberWithInt:[contentOfFirstPlistFile count]]

Gives you an NSNumber object. This is not a valid key for a dictionary, the key has to be an NSString. Use

[NSString stringWithFormat:@"%d",[contentOfFirstPlistFile count]]

Instead. Though, if you are using numbers as keys in a dictionary, why not use an array?

I have attempted to reproduce this issue myself and the only way I could manage it was to use a plist that did not have a dictionary as its root object. This returned a dictionary that was not nil, but did not allow any modifications. This could not then be written back to the file. Looking at some of your earlier questions, you were using an array based plist before, so this may be the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消