2

I've been trying to save a plist of a NSDictionary to my app's Documents folder. I haven't tried this on the device yet but I'd like it to work on the simulator for testing purposes. The [self createDictionaryFromChoreList] method just creates a NSDictionary from some data in another class of mine. I've pretty much copied/pasted this code from the web documents and when I go to see if the file was saved or not, I find that it isn't. Here is the method block.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistName = [[NSString alloc] initWithFormat:@"%@chores.plist", self.firstName];
NSString *path = [documentsDirectory stringByAppendingPathComponent:plistName];

NSDictionary *choresDictionary = [[NSDictionary alloc] initWithDictionary:[self createDictionaryFromChoreList]];
[choresDictionary writeToFile:path atomically:YES];

Any help is greatly appreciated. Thanks in advance.

-S

skylerl
  • 4,030
  • 12
  • 41
  • 60

7 Answers7

5

You should also capture the BOOL returned by writeToFile:atomically:. That will tell you if the write succeeded or not.

Also, are you sure you are looking in the right documents folder? If you have more than one app in the simulator its easy to open the wrong app's documents folder in the Finder. I did that once and it cost me a couple of hours of frustration.

Edit01:

writeToFile:atomically: returning false explains why no file exist. The simplest explanation is that something in the dictionary is not a property list object.

From the NSDictionary docs:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

It just takes one non-plist object buried deep in a dictionary to prevent it from being converted to a plist.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • I'll try capturing the boolean value returned by writeToFile. I do know it's the right Documents folder, I've made some breakpoints and looked into the objects and they do have the right location. Edit: It is returning false and I know I'm looking in the right folder because I've only run one app in the simulator so far. (Had to reformat). – skylerl Jan 22 '10 at 02:39
1

This is something I whipped up really quickly and it correctly writes a plist directory of name and company to the documents directory. I have a feeling your dictionary creation method might have an issue. Try this out for yourself, then add your code and make sure it works.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistDirectory = [paths objectAtIndex:0];
NSString *plistPath = [plistDirectory stringByAppendingPathComponent:@"userCompany.plist"];

NSArray *userObjects = [[NSArray alloc] initWithObjects:@"Joe", @"Smith", @"Smith Co", nil];
NSArray *userKeys = [[NSArray alloc] initWithObjects:@"First Name", @"Last Name", @"Company", nil];

NSDictionary *userSettings = [[NSDictionary alloc] initWithObjects:userObjects forKeys:userKeys];

[userSettings writeToFile:plistPath atomically:YES];
M-P
  • 4,909
  • 3
  • 25
  • 31
Convolution
  • 2,351
  • 17
  • 24
  • The path is: path: /Users/slehan/Library/Application Support/iPhone Simulator/User/Applications/6BBA0A58-E759-44C1-B018-829EBC5254AD/Documents/namechores.plist However when I look there, there is no file. – skylerl Jan 22 '10 at 11:47
1

Don't forget serialize the plist data:

Here is a snippet of code that I use for writing information to a plist

NSString *errorString;

NSData *data = [NSPropertyListSerialization dataFromPropertyList:plistDict 
                                                       format:NSPropertyListXMLFormat_v1_0 
                                              errorDescription:&errorString];
[plistDict release];

if (!data) {
  NSLog(@"error converting data: %@", errorString);
  return NO;    
}

if ([data writeToFile:[XEraseAppDelegate loadSessionPlist] atomically: YES]) {
  return YES;
} else {

   NSLog(@"couldn't write to new plist");

 return NO;
}
Cory D. Wiles
  • 556
  • 1
  • 6
  • 21
0

Is it correct, that the name of file your writing to is: SOEMTHINGchores.plist?

Created via:

NSString *plistName = [[NSString alloc] initWithFormat:@"%@chores.plist", self.firstName];

Also, what is the output of:

[choresDictionary print];

Some additional info would help to debug this.

mr-sk
  • 13,174
  • 11
  • 66
  • 101
0

Where exactly are you looking for the file?

I have the exact same code and it works fine for me.

Just that I have to dig deep to get the file. Something like:

/Users/myUserName/Library/Application Support/iPhone Simulator/User/Applications/0E62A607-8EEB-4970-B198-81CE4BDDB7AA/Documents/data.plist

And the HEX number in the path changes with every run. So I print the file path with every run.

Mihir Mathuria
  • 6,479
  • 1
  • 22
  • 15
0

Insert a break point at

NSDictionary *choresDictionary = [[NSDictionary alloc] initWithDictionary:[self createDictionaryFromChoreList]];

now when you step out drag your mouse over choresDictionary and check in the tooltip that its size is not 0x0 or you can simply do an NSLog of the choresDictionary like NSLog(@"%@",choresDictionary); I think your dictionary has 0 key key value pairs thats why you are getting null into your documents folder.

Thanks,

Madhup

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
0

I was running into this issue as well. In my case it turned out that I was using NSNumbers for keys - which is not valid.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Kevin
  • 1