3

I know there are tons of threads already on this issue, but I cannot seem to find one that solves my problem. I have a plist with a dictionary as the root, containing three arrays. My code to write to the plist works fine in the simulator but is (null) on the device.

  • I'm not trying to write to the app bundle,
  • My file path is correct and I am checking at launch to make sure the file exists in the Documents folder (and it does exist).

    - (void) writeToPlist:(NSString *)fileName playerColor:(NSString *)player withData:(NSArray *)data
    {
        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
        NSString *documentsDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:fileName];
    
        NSLog(@"File Path: %@", filePath);
    
        NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    
        NSLog(@"plist: %@", [plistDict description]);
    
        [plistDict setValue:data forKey:player];
    
        BOOL didWriteToFile = [plistDict writeToFile:filePath atomically:YES];
        if (didWriteToFile) {
            NSLog(@"Write to file a SUCCESS!");
        } else {
            NSLog(@"Write to file a FAILURE!");
        }
    }
    

Debug output:

    File Path: /var/mobile/Applications/CA9D8884-2E92-48A5-AA73-5252873D2571/Documents/CurrentScores.plist
    plist: (null)
    Write to file a FAILURE!

I've used this same method in other projects, so I don't know if I'm forgetting something or what the deal is. I've checked spelling/capitalization and remade the plist, neither made any difference.

So why is plistDict (null) on the device, but not on the simulator? I apologize if I missed the solution in another post on plists.

4

1 回答 1

2

您的代码被编写为假定该文件已存在于Documents文件夹中。第一次调用此方法时不会出现这种情况。

您应该添加检查文件是否存在。如果它在那里,请加载它。如果没有,请对数据进行一些其他适当的初始化以准备写入。

此外,您的字典需要是可变的,以便您更改或添加键/值。

- (void) writeToPlist:(NSString *)fileName playerColor:(NSString *)player withData:(NSArray *)data
{
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
    NSString *documentsDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:fileName];

    NSLog(@"File Path: %@", filePath);

    NSMutableDictionary *plistDict; // needs to be mutable
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    } else {
        // Doesn't exist, start with an empty dictionary
        plistDict = [[NSMutableDictionary alloc] init];
    }

    NSLog(@"plist: %@", [plistDict description]);

    [plistDict setValue:data forKey:player];

    BOOL didWriteToFile = [plistDict writeToFile:filePath atomically:YES];
    if (didWriteToFile) {
        NSLog(@"Write to file a SUCCESS!");
    } else {
        NSLog(@"Write to file a FAILURE!");
    }
}
于 2013-05-09T22:56:04.740 回答