2012-03-15 62 views
1

我有一個對象數組,我想保存爲一個文件並重新加載到我的應用程序。它正在保存文件(其中包含一些數據),但我無法將其讀回到NSMutable Array中。無法獲取NSData到NSMutableArray

的對象是符合NSCoding協議模型:

@implementation myModel 

@synthesize name; 
@synthesize number; 

-(void) encodeWithCoder: (NSCoder *) encoder 
{ 
    [encoder encodeObject:name forKey:@"name"]; 
    [encoder encodeInteger:number forKey:@"number"]; 
} 

-(id) initWithCoder: (NSCoder *) decoder 
{ 
    name = [decoder decodeObjectForKey:@"name"]; 
    number = [decoder decodeIntegerForKey:@"number"]; 
    return self; 
} 

@end 

所以我創建這些對象的數組,然後我將它保存...

- (void) saveMyOptions { 

    // Figure out where we're going to save the app's data files 
    NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user 

    // Figure out if that directory exists or not 
    BOOL isDir; 

    NSFileManager *fileManager = [[NSFileManager alloc] init]; 

    [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir]; 

    // If the directory doesn't exist, create it 
    if (!isDir) 
    { 
     [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:NULL]; 
    } 

    // Assemble everything into an array of objects with options 
    NSMutableArray *savedPreferences = [[NSMutableArray alloc] init]; 

    myModel *saveOptions = nil; 

    for (int i; i < [otherArray count]; i++) 
    { 
     saveOptions = [[myModel alloc] init]; 

     [saveOptions setName:@"Some String"]; 
     [saveOptions setNumber:i];   
     [savedPreferences addObject:saveOptions]; 
     saveOptions = nil; 
    } 

    // Actually save those options into a file 
    NSData* saveData = [NSKeyedArchiver archivedDataWithRootObject:savedPreferences]; 

    NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath]; 

    NSError *error = nil; 

    BOOL written = [saveData writeToFile:fileName options:0 error:&error]; 

    if (!written) 
    { 
     NSLog(@"Error writing file: %@", [error localizedDescription]); 
    } 

} 

所以,現在我試着將該數據加載回數組中。這是我認爲它崩潰的地方...

- (NSMutableArray *) loadOptions { 

    // Create file manager object 
    NSFileManager *fileManager = [[NSFileManager alloc] init]; 

    NSData *saveData = nil; 

    // Find user directory path 
    NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user 

    // Assign file name 
    NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath]; 

    // Create options array 
    NSMutableArray *myOptions = nil; 

    // If the file exists, fill the array with options 
    if ([fileManager fileExistsAtPath:fileName]) 
    { 
     saveData = [NSData dataWithContentsOfFile:fileName]; 
     myOptions = [NSKeyedUnarchiver unarchiveObjectWithData:saveData]; 
    } 


    NSLog(@"%lu", [myOptions count]); // This ALWAYS reports 0! 
    NSLog(@"%lu", [saveData length]); // This reports a value of 236; 

    return myOptions; 
} 

有人能指點我的方向,我要去哪裏錯了嗎?我throughly困惑:-(

提前感謝!

回答

1

你缺少你encodeWithCoder:initWithCoder:方法super調用,但是這只是一個猜測。爲什麼不使用NSUserDefaults保存偏好?

您可能還需要確保你的對象將保留使用合成器設置。

- (void)encodeWithCoder:(NSCoder *)encoder { 
    [super encodeWithCoder:encoder]; 
    [encoder encodeObject:name forKey:@"name"]; 
    [encoder encodeInteger:number forKey:@"number"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    self = [super initWithCoder:decoder]; 
    if (self) { 
    self.name = [decoder decodeObjectForKey:@"name"]; 
    self.number = [decoder decodeIntegerForKey:@"number"]; 
    } 
    return self; 
} 

您的信息,NSKeyedArchiver也有方法你可以直接使用對文件進行操作:

+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path 

NSKeyedUnarchiver

+ (id)unarchiveObjectWithFile:(NSString *)path 
+0

超肯定是它的一部分。謝謝你!第二部分是因爲,像一個白癡一樣,當我循環使用我想要保存的對象來填充數組時,我沒有分配i = 0。因此,循環從未跑,因爲我是零...很好。非常感謝你的幫助! – 2012-03-16 01:42:37

+0

甚至沒有看到缺少'我'分配。 – 2012-03-16 18:02:14