Why NSDictionary writeToFile fails if keys are NSDate objects

Hi,
I am trying to use writeToFile from NSDictionary and I cannot get it working. I have made the following test:

NSDate *key = [[NSDate alloc] init];
NSArray *test = [[NSArray alloc] initWithObjects:@"Easy", key, nil];
[test writeToFile:[self dataFilePath] atomically:YES];

Which perfectly works and returns YES

But if I do:
NSDate *key = [[NSDate alloc]init];
NSArray *test = [[NSArray alloc] initWithObjects:@"Easy",key, nil];
NSDictionary *testDict = [[NSDictionary alloc] initWithObjectsAndKeys:test,key,nil];
[testDict writeToFile:[self dataFilePath] atomically:YES];

It fails. Why is that? Why is I use a NSDate as a key it fails writing into a file but I can work with this Dictionary in everything else?

The following code works:
NSDate *key = [[NSDate alloc]init];
NSArray *test = [[NSArray alloc] initWithObjects:@"Easy",key, nil];
NSDictionary *testDict = [[NSDictionary alloc] initWithObjectsAndKeys:test,@"Value1",key,"@Value2",nil];
[testDict writeToFile:[self dataFilePath] atomically:YES];

Thanks in advance

Mac Mini

Posted on Oct 16, 2009 5:54 AM

Reply
2 replies

Oct 16, 2009 7:49 AM in response to fspace

writeToFile: will only work if the entire contents of the NSDictionary are plistable items, like NSString, NSData, NSArray, NSDictionary, or their mutable subclasses.

If you are trying to archive the dictionary so you can read it back in later, us NSArchiver, and you can archive just about any object.

If you are trying to just get a human readable version into a file for debugging, convert the dict to a string using description:

[[test description] writeToFile:[self dataFilePath]]

Oct 16, 2009 4:58 PM in response to fspace

To quote from the NSDictionary reference:{quote}
A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:). In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that *when using key-value coding the key must be a string* (see Key-Value Coding Fundamentals). Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.
{quote}

Since writing a dictionary out to a file relies on key-value coding, I believe that the problem is as simple as requiring the key to be a string (or a string representation of your object). Your Object that the dictionary holds can be an NSDate object, but not the key itself.

At least that is what I would understand reading the docs.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Why NSDictionary writeToFile fails if keys are NSDate objects

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.