1
votes

I am trying to figure out how to save a NSDate value into my plist file thats in my application.

I am currently doing this but am stuck on the actual part where I have to save it.

NSString *datePlistPath = [[NSBundle mainBundle] pathForResource: @"my-Date" ofType: @"plist"];
        NSMutableDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: datePlistPath];

        // to be saved to plist
        NSDate *date = [NSDate date];

// this is where I start to get abit lost, I want to set the date to the right plist value then commit the changes
        [dict setObject:date forKey:@"my-Date"];
        [dict writeToFile:datePlistPath atomically:YES]; // error happening here.

any help would be appreciated

UPDATE: once it hits the last line of code there this is the error that is generated...

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

5
does the datePlistPath(filePath) exists?cloosen
yep, I know this because I am able to read from it :P I have updated with the error I am getting in my logg.HurkNburkS

5 Answers

3
votes

Use NSMutableDictionary dictionaryWithContentsOfFile

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile: datePlistPath];

it provide u NSDictionary not NSMutableDictionary if u use NSDictionary dictionaryWithContentsOfFile

Also u cannot update plist in application bundle instead store in Document Directory.

Refer dit-objects-in-array-from-plist link

1
votes

I think solution in your case is simple like

Replace

NSMutableDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: datePlistPath];

with

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile: datePlistPath];
1
votes
NSMutableDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: datePlistPath];

replace NSDictionary with NSMutableDictionary.

0
votes

You cannot write a file to

NSBundle

. Files can be stored only in Documents directory, temp directory and some pre defined locations. you can try the following code. It worked fine for me.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"file.plist"];

NSDate *currentDate = [NSDate date];
NSMutableDictionary *d = [NSMutableDictionary new];
[d setObject:currentDate forKey:@"my-date"];

[d writeToFile:filePath atomically:YES];
0
votes

You can't write to files in your own bundle. You don't say exactly what you are using the date for, but if you just want to persist this one NSDate across launches, you probably want to write this date to NSUserDefaults.

Docs here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

Your code to write it would just look like:

[[NSUserDefaults standardUserDefaults] setObject:date forKey:@"my-Date"];

and to read it back you would do

NSDate* myDate = (NSDate*)[[NSUserDefaults standardUserDefaults] objectForKey:@"my-Date"];
// Be prepared for a nil value if this has never been set.