1
我正在嘗試使用來創建應用程序目標C。序列化目標C不能正常工作
我想序列化存在於對象之外的數組,並在病房之後進行反序列化。對象裏面有方法
(id)initWithCoder:(NSCoder *)coder` and `encodeWithCoder:(NSCoder *)coder
但似乎「rootObject」住宿「無」在「loadDataFromDisk」 - 方法
這裏是我的代碼:
#import "Alarm.h"
@implementation Alarm
@synthesize array = _array;
@synthesize time = _time;
@synthesize coder = _coder;
-(id)initWithCoder:(NSCoder *)coder
{
self = [super init];
if(self)
{
_array = [coder decodeObjectForKey:@"array"];
_time = [coder decodeObjectForKey:@"time"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.array forKey:@"array"];
[coder encodeObject:self.time forKey:@"time"];
}
@end
我的保存和加載方法:
-(void)saveDataToDisk
{
NSString * path = [self pathForDataFile];
NSLog(@"Writing alarms to '%@' %lu", path, (unsigned long)array.count);
NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue:array forKey:@"alarms"];
[NSKeyedArchiver archiveRootObject:rootObject toFile:path];
}
-(void)loadDataFromDisk
{
NSString *path = [self pathForDataFile];
NSDictionary *rootObject = [NSMutableDictionary dictionary];
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// "array" is an array with Objects of "Alarm"
array = [rootObject valueForKey:@"alarms"];
NSLog(@"Loaded from : %@ %lu",path ,(unsigned long)array.count);
}
我希望任何人都可以幫助我解決這個問題。 在此先感謝。
注意:'@ synthesisize'已經有好幾年了。 – zaph 2014-10-30 13:41:51
'array'中有什麼樣的對象?你是否檢查過寫入文件的內容? – zaph 2014-10-30 13:42:39