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.

1
  • Does the file exist? It won't the first time you ever call this method.
    – rmaddy
    May 9, 2013 at 22:53

1 Answer 1

2

Your code is written to assume the file already exists in the Documents folder. This won't be the case the first time this method is ever called.

You should add a check for the existence of the file. If it's there, load it. If not, do some other proper initialization of your data in preparation for writing.

Also, your dictionary needs to be mutable for you to change or add keys/values.

- (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!");
    }
}
3
  • If that's the case wouldn't his simulator build not work as well? May 9, 2013 at 22:57
  • 1
    @MarkM Normally but the file may have been added to the simulator through some other means in the past. This is why when preparing to submit an app to Apple for release, you must always delete the app from a device and do a clean install. Then fully test the app to be sure the app handles a clean start.
    – rmaddy
    May 9, 2013 at 23:01
  • Thanks, that did the trick! As I suspected, it was something simple that I was overlooking. I thought checking for the writable path at app launch would have been sufficient, but I guess not. May 9, 2013 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.