views:

580

answers:

6

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

A: 

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
Yes, that's correct. And NSDictionary doesn't have a print method.
Yakattak
A: 

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 *fullPath = [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];
Convolution
The path is: path: /Users/slehan/Library/Application Support/iPhone Simulator/User/Applications/6BBA0A58-E759-44C1-B018-829EBC5254AD/Documents/namechores.plistHowever when I look there, there is no file.
Yakattak
+2  A: 

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
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).
Yakattak
A: 

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 Wiles
A: 

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
I'm looking in a likewise area; I just don't think it's writing at all.
Yakattak
A: 

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