2

I am trying to save some values from my app using NSCoding. I'm able to save the value but not able to retrieve it.

Here's where I am declaring the protocol:

@interface AddReminderEventViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, NSCoding> 

Here's where I'm complying with the protocol:

-(void)encodeWithCoder:(NSCoder *)enCoder
{
[enCoder encodeObject:self.eventRepeatDurationDate   forKey:kEventRepeatDuration];
[enCoder encodeObject:self.eventIDsMutableArray      forKey:kEventIDsMutableArray];
[enCoder encodeObject:self.eventRepeatDurationString forKey:@"mytest"];}

and here:

-(id)initWithCoder:(NSCoder *)decoder {

if (self = [super init]){

    self.eventRepeatDurationDate = [[decoder decodeObjectForKey:kEventRepeatDuration]  retain];
    self.eventIDsMutableArray    = [[decoder decodeObjectForKey:kEventIDsMutableArray] retain];
    self.eventRepeatDurationString = [[decoder decodeObjectForKey:@"mytest"]  retain];} return self; }

and here's where I call the methods to do the archiving and unarchiving:

    [self saveDataToDisk];
    [self loadDataFromDisk];

and here are the bodies of these methods and it's NSLog contents:

- (void)saveDataToDisk {
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";    
//reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH1: reminderEventIDsPathString is %@", reminderEventIDsPathString);

NSMutableDictionary *rootObject;
rootObject = [NSMutableDictionary dictionary];

[rootObject setValue:eventRepeatDurationString forKey:@"mytest"];
NSLog(@"1rootObject IS %@", rootObject);

[NSKeyedArchiver archiveRootObject:rootObject toFile:reminderEventIDsPathString];}

reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive
2012-01-16 15:47:48.578 [29658:15503] 1rootObject IS {mytest = 7;}

and here is the unarchiver code along with its NSLog contents:

- (void)loadDataFromDisk {
NSString *testValue = [[NSString alloc] init];
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";    
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH2: reminderEventIDsPathString is %@", reminderEventIDsPathString);

NSMutableDictionary *rootObject;
rootObject = [[NSKeyedUnarchiver unarchiveObjectWithFile:reminderEventIDsPathString] retain];

NSLog(@"2rootObject IS %@", rootObject);

NSLog(@"WATCH3 - %@", [rootObject objectForKey:@"mytest" ]);

if ([rootObject valueForKey:@"mytest"]) {
    testValue = [rootObject valueForKey:@"mytest"];
    NSLog(@"WATCH: testValue is %@", testValue); } }

2012-01-16 15:48:14.965 [29658:15503] WATCH2: reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive

2012-01-16 15:48:17.879 [29658:15503] 2rootObject IS (null)

What am I missing that I'm not able to unarchive the contents? I'm just focusing on the easiest of the values in my encoder/decoder methods just to test it but I'm not even able to get the string value to work.

Thanks

Jazzmine
  • 1,837
  • 8
  • 36
  • 54

1 Answers1

1

The path where you save and load your reminder is wrong. Maybe replace to this

NSString *reminderEventIDsPathString = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ReminderIDs.archive"];
X Slash
  • 4,133
  • 4
  • 27
  • 35
  • Can you tell me what you mean by 'wrong'? I thought I was supposed to write to the Library path. Is that incorrect? Also, I see the file at that path and it has contents in it. Thanks – Jazzmine Jan 16 '12 at 22:33
  • Well, that worked! Thank you so much but I would also appreciate your clarifying why that worked. Are apps not supposed to write to/read from the path I had? Thanks for taking the time to answer. – Jazzmine Jan 16 '12 at 22:41
  • Also, if I want to write the value out in one view controller and retrieve it in another, should I be using/referencing the appDelegate to do the writing and reading to avoid duplication of code? Thanks again. – Jazzmine Jan 16 '12 at 22:45
  • apps are sandboxed, they're only supposed to write and read from their own sandbox, which can be easily accessed with NSHomeDirectory(), the path you had earlier was out of your app's sandbox. If you want to write into "Library", just replace the "Documents" to "Library". – X Slash Jan 16 '12 at 22:53
  • If you go to ~/Library/Application Support/iPhone Simulator/5.0/Applications/ you can see folders with random name, and inside those folders are Documents, Library, tmp. Each app will have their own space, and can only read and write there – X Slash Jan 16 '12 at 22:57