Domanda

Sto cercando di salvare alcuni valori dalla mia app utilizzando NSCoding.Sono in grado di salvare il valore ma non di recuperarlo.

Ecco dove dichiaro il protocollo:

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

Ecco dove sto rispettando il protocollo:

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

e qui:

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

ed è qui che chiamo i metodi per eseguire l'archiviazione e l'annullamento dell'archiviazione:

    [self saveDataToDisk];
    [self loadDataFromDisk];

e qui ci sono i corpi di questi metodi e i suoi contenuti NSLog:

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

ed ecco il codice unarchiver insieme ai suoi contenuti NSLog:

- (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)

Cosa mi manca se non riesco a disarchiviare i contenuti?Mi sto solo concentrando sul più semplice dei valori nei miei metodi codificatore / decodificatore solo per testarlo, ma non sono nemmeno in grado di far funzionare il valore della stringa.

Grazie

È stato utile?

Soluzione

Il percorso in cui salvi e carichi il promemoria è sbagliato.Forse sostituire a questo

NSString *reminderEventIDsPathString = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ReminderIDs.archive"];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top