我想從NSUserdefaults解碼我的對象。 如果我調試函數initWithCoder,它的執行沒問題,但是當我查看解碼的對象時,所有的屬性都是零。無法解碼從NSUserDefaults對象
我的initWithCoder功能:
-(id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self != nil)
{
//decode the properties
self.SGTIN = [decoder decodeObjectForKey:@"SGTIN"];
self.GTIN = [decoder decodeObjectForKey:@"GTIN"];
self.SerialNumber = [decoder decodeObjectForKey:@"SerialNumber"];
self.Product = [decoder decodeObjectForKey:@"Product"];
self.DateTimeScanned = [decoder decodeObjectForKey:@"DateTimeScanned"];
self.Loaded = [decoder decodeBoolForKey:@"Loaded"];
}
return self;
}
我的代碼的對象進行解碼: 的NSDictionary * tempDictionary = [[NSUserDefaults的standardUserDefaults] dictionaryRepresentation];
NSArray *tempArray =[tempDictionary allKeys];
for (NSString *key in tempArray){
if ([key length] > 8) {
if ([[key substringToIndex:8] isEqualToString:@"INXITEM:"]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [defaults objectForKey:key];
ScannedItem *SITemp = (ScannedItem *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
}
}
}
只是覺得...
這是因爲屬性加載(布爾)是一個新的屬性,並且保存在NSUserDefaults的不包含屬性的對象。
清除我和NSUserDefaults的一切,現在是好...
什麼是ScannedItem'的'超。何時以及如何對實例進行編碼?你是否正確實施了編碼? – Wain