Plist not saving from dictionary (to Documents)

萝らか妹 提交于 2019-12-13 16:25:25

问题


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


回答1:


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.




回答2:


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;
}



回答3:


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];



回答4:


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.




回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/2114444/plist-not-saving-from-dictionary-to-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!