2016-05-14 12 views
0

Ich versuche, einige persistente Daten in einer iOS-App mit NSKeyedArchiver zu speichern, um in eine Datei zu schreiben, und ich möchte diese Daten später mit NSKeyedUnarchiver abrufen. Ich habe eine sehr einfache Anwendung erstellt, um Code zu testen, aber ohne Erfolg. Hier sind die Methoden, die ich benutze:Fehler beim Festlegen/Abrufen von Daten mit NSKeyedArchiver/NSKeyedUnarchiver in Datei

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    Note *myNote = [self loadNote]; 

    myNote.author = @"MY NAME"; 

    [self saveNote:myNote]; // Trying to save this note containing author's name 

    myNote = [self loadNote]; // Trying to retrieve the note saved to the file 

    NSLog(@"%@", myNote.author); // Always logs (null) after loading data 
} 

-(NSString *)filePath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent: @"myFile"]; 
    return filePath; 
} 

-(void)saveNote:(Note*)note 
{ 
    bool success = [NSKeyedArchiver archiveRootObject:note toFile:[self filePath]]; 
    NSLog(@"%i", success); // This line logs 1 (success) 
} 

-(Note *)loadNote 
{ 
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath]]; 
} 

Die Klasse, die ich diesen Code zu testen verwende ist wie folgt:

Note.h

#import <Foundation/Foundation.h> 

@interface Note : NSObject <NSCoding> 

@property NSString *title; 
@property NSString *author; 
@property bool published; 

@end 

Note.m

#import "Note.h" 

@implementation Note 

-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super init]) 
    { 
     self.title = [aDecoder decodeObjectForKey:@"title"]; 
     self.author = [aDecoder decodeObjectForKey:@"author"]; 
     self.published = [aDecoder decodeBoolForKey:@"published"]; 
    } 
    return self; 
} 

-(void)encodeWithCoder:(NSCoder *)aCoder 
{ 
    [aCoder encodeObject:self.title forKey:@"title"]; 
    [aCoder encodeObject:self.author forKey:@"author"]; 
    [aCoder encodeBool:self.published forKey:@"published"]; 
} 

@end 

Ich habe ähnliche Beispiele mit NSUserDefaults (https://blog.soff.es/archiving-objective-c-objects-with-nscoding), bu t Ich möchte diese Daten in einer Datei speichern, da NSUserDefaults, soweit ich weiß, hauptsächlich zum Speichern von Benutzereinstellungen und nicht für allgemeine Daten verwendet wird. Fehle ich etwas? Danke im Voraus.

Antwort

0

Denken Sie darüber nach, was passiert, wenn Ihre App das erste Mal ausgeführt wird und Sie Ihre loadNote:-Methode aufrufen und noch nichts gespeichert ist.

Die Linie:

Note *myNote = [self loadNote]; 

führt in myNote sein nil da nichts geladen wurde. Denken Sie nun darüber nach, wie das den Rest Ihres Codes durchläuft.

Sie müssen diesen ersten Fall behandeln, in dem keine gespeicherten Daten vorhanden sind.

Note *myNote = [self loadNote]; 
if (!myNote) { 
    myNote = [[Note alloc] init]; 
    // Setup the initial note as needed 
    myNote.title = ... 
}