我試圖將我的對象圖保存到一個文件,然後在稍後重新加載它,但是decodeObjectForKey:對於我指定的任何鍵總是返回nil。decodeObjectForKey:總是返回零
創建了一個二進制文件,它偶爾還有人類可讀的文本,即titleTextColor,所以我認爲歸檔過程正在工作。
我想知道NSKeyedArchiver和NSKeyedUnarchiver的工作原理嗎?任何幫助,將不勝感激。
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeFloat:titleFontSize forKey:@"titleFontSize"];
[encoder encodeObject:[UIColor orangeColor] forKey:@"titleTextColor"];
[encoder encodeObject:lineSeparatorColor forKey:@"lineSeparatorColor"];
[encoder encodeObject:bodyTextColor forKey:@"bodyTextColor"];
[encoder encodeFloat:bodyTextFontSize forKey:@"bodyTextFontSize"];
[encoder encodeObject:backgroundColor forKey:@"backgroundColor"];
[encoder encodeObject:tintColor forKey:@"tintColor"];
[encoder encodeInteger:bodyTextAlignment forKey:@"bodyTextAlignment"];
[encoder encodeObject:@"Text" forKey:@"Text"];
}
+ (void) saveToFile {
// Get the shared instance
PSYDefaults *sharedInstance = [PSYDefaults sharedInstance];
// Serialise the object
NSData *serializedObject = [NSKeyedArchiver archivedDataWithRootObject:sharedInstance];
// Get the path and filename of the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathAndFileName = [documentsDirectory stringByAppendingPathComponent:ksDefaultsFileName];
// Write the defaults to a file
if (serializedObject) [serializedObject writeToFile:pathAndFileName atomically:YES];
}
+ (void) loadFromFile {
// Get the shared instance
PSYDefaults *sharedInstance = [PSYDefaults sharedInstance];
// Get the path and filename of the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathAndFileName = [documentsDirectory stringByAppendingPathComponent:ksDefaultsFileName];
NSData *codedData = [[[NSData alloc] initWithContentsOfFile:pathAndFileName] autorelease];
NSKeyedUnarchiver *defaults = [[NSKeyedUnarchiver alloc] initForReadingWithData:codedData];
// Set the properties of the shared instance
NSString *test = [defaults decodeObjectForKey:@"Text"];
NSLog (@"%@", test);
sharedInstance.titleTextColor = [defaults decodeObjectForKey:@"titleTextColor"];
[defaults release];
}
編輯:根據意見從DarkDust:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.titleFontSize = [[aDecoder decodeObjectForKey:@"titleFontSize"] floatValue];
self.titleTextColor = [aDecoder decodeObjectForKey:@"titleTextColor"];
self.lineSeparatorColor = [aDecoder decodeObjectForKey:@"lineSeparatorColor"];
self.bodyTextColor = [aDecoder decodeObjectForKey:@"bodyTextColor"];
self.bodyTextFontSize = [[aDecoder decodeObjectForKey:@"bodyTextFontSize"] floatValue];
self.backgroundColor = [aDecoder decodeObjectForKey:@"backgroundColor"];
self.tintColor = [aDecoder decodeObjectForKey:@"tintColor"];
self.bodyTextAlignment = [[aDecoder decodeObjectForKey:@"bodyTextAlignment"] intValue];
}
return self;
}
,只是測試創建一個新的實例:
+ (void) loadFromFile {
// Get the shared instance
PSYDefaults *sharedInstance = [PSYDefaults sharedInstance];
// Get the path and filename of the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathAndFileName = [documentsDirectory stringByAppendingPathComponent:ksDefaultsFileName];
NSData *codedData = [[[NSData alloc] initWithContentsOfFile:pathAndFileName] autorelease];
PSYDefaults *newInstance = [NSKeyedUnarchiver unarchiveObjectWithData:codedData];
sharedInstance.titleTextColor = newInstance.titleTextColor;
}
編輯 - 更新(需要編碼花車和ints as NSNumbers)
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:[NSNumber numberWithFloat:titleFontSize] forKey:@"titleFontSize"];
[encoder encodeObject:titleTextColor forKey:@"titleTextColor"];
[encoder encodeObject:lineSeparatorColor forKey:@"lineSeparatorColor"];
[encoder encodeObject:bodyTextColor forKey:@"bodyTextColor"];
[encoder encodeObject:[NSNumber numberWithFloat:bodyTextFontSize] forKey:@"bodyTextFontSize"];
[encoder encodeObject:backgroundColor forKey:@"backgroundColor"];
[encoder encodeObject:tintColor forKey:@"tintColor"];
[encoder encodeObject:[NSNumber numberWithInt:bodyTextAlignment] forKey:@"bodyTextAlignment"];
}
這是很好的建議,謝謝。我意識到我的愚蠢與unarchiving,現在創建一個新的實例,並實施了一個initWithCoder。我認爲我快到了,當NSKeyedUnarchiver取消存檔我的對象圖時,initWithCoder被調用。然而,我在調用initWithCoder之後遇到任何問題? Dave – 2012-02-24 10:10:18
Doh,擦洗。當它已經自動發佈時釋放newInstance是問題。感謝你的幫助!戴夫 – 2012-02-24 10:14:14