1
我有一些自定義類對象存儲在一個數組中,然後將此數組存儲在帶有鍵的字典中。然後保存並在應用程序加載/重新加載時加載。存儲在plist中的數組中的自定義對象
現在的問題是,我似乎無法取回自定義對象,當我加載數組它回來爲空。我必須在寫或加載數據時出錯,但我無法弄清楚究竟是什麼。
下面的代碼我到目前爲止:
CustomClass.H文件:
#import <Foundation/Foundation.h>
@interface PersonClass : NSObject <NSCoding>
@property (nonatomic,strong) NSString *personName;
@property (nonatomic,strong) NSString *personNo;
@property (nonatomic,strong) NSString *personNotes;
@property (nonatomic) BOOL switchPersonality;
@property (nonatomic) BOOL switchChemistry;
@property (nonatomic) BOOL switchLooks;
@property (nonatomic) BOOL switchHumour;
@property (nonatomic) int personRating;
@property (nonatomic,strong) NSNumber *personRecord;
- (id)initWithName:(NSString *)iPersonName PersonNo:(NSString *)iPersonNo PersonNotes:(NSString *)iPersonNotes SwitchChemistry:(BOOL *)iSwitchChemistry SwitchHumour:(BOOL *)iSwitchHumour SwitchLooks:(BOOL *)iSwitchLooks SwitchPersonality:(BOOL *)iSwitchPersonality PersonRating:(int *)iPersonRating PersonRecord:(NSNumber *)iPersonRecord;
@end
定製Class.M文件:
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:personName forKey:@"pName"];
[aCoder encodeObject:personNo forKey:@"pNo"];
[aCoder encodeObject:personNotes forKey:@"pNotes"];
[aCoder encodeInt:personRating forKey:@"pRating"];
[aCoder encodeObject:personRecord forKey:@"pRecord"];
[aCoder encodeBool:switchChemistry forKey:@"sChemistry"];
[aCoder encodeBool:switchHumour forKey:@"sHumour"];
[aCoder encodeBool:switchLooks forKey:@"sLooks"];
[aCoder encodeBool:switchPersonality forKey:@"sPersonality"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]){
self.personName = [aDecoder decodeObjectForKey:@"pName"];
self.personNo = [aDecoder decodeObjectForKey:@"pNo"];
self.personNotes = [aDecoder decodeObjectForKey:@"pNotes"];
self.personRating = [aDecoder decodeIntForKey:@"pRating"];
self.personRecord = [aDecoder decodeObjectForKey:@"pRecord"];
self.switchChemistry = [aDecoder decodeBoolForKey:@"sChemistry"];
self.switchHumour = [aDecoder decodeBoolForKey:@"sHumour"];
self.switchLooks = [aDecoder decodeBoolForKey:@"sLooks"];
self.switchPersonality = [aDecoder decodeBoolForKey:@"sPersonality"];
}
return self;
}
保存方法:
- (void)saveData{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
NSLog(@"PList Path %@",plistPath);
//new array
NSMutableArray *newEntries = [[NSMutableArray alloc]init];
NSLog(@"NEW ENTRIES BEFORE %@",newEntries);
for (PersonClass *person in peopleEntries) {
//encode the object
[NSKeyedArchiver archivedDataWithRootObject:person];
// add the object to the entries
[newEntries addObject:person];
}
NSLog(@"NEW ENTRIES AFTER %@",newEntries);
[newEntries writeToFile:plistPath atomically:YES];
// create dictionary with arrays and their corresponding keys
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSMutableArray arrayWithObjects:newEntries, recordId, nil] forKeys:[NSMutableArray arrayWithObjects: @"peopleEntries",@"recordId", nil]];
NSLog(@"DICTIONARY IS IN SAVE %@",plistDict);
NSString *error = nil;
// check if plistData exists
if(plistDict)
{
// write plistData to our Data.plist file
[plistDict writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
}
NSLog(@"Save RUN");
}
加載方法:
- (void)loadData{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
NSLog(@"PList Path %@",plistPath);
// check to see if data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// return without loading
NSLog(@"RETURNING");
return;
}
// get saved dictionary
NSDictionary *dictionaryTemp = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@"DICTIONARY IS LOAD %@",dictionaryTemp);
if (!dictionaryTemp)
{
NSLog(@"Error reading plist:");
}
// temporary array
NSMutableArray *holderOne = [dictionaryTemp objectForKey:@"peopleEntries"];
// array to be populated
NSMutableArray *holderTwo = [[NSMutableArray alloc]init];
NSLog(@"HOLDER ONE IS BEFORE %@",holderOne);
NSLog(@"HOLDER TWO IS BEFORE %@",holderTwo);
// go through the array and pull out person classes
for (NSData *person in holderOne) {
// temp array
NSData *entry = [holderOne objectAtIndex:person]; // check the object at index might be an issue???
NSLog(@"ENTRY IS %@",entry);
//deencode the object
[NSKeyedUnarchiver unarchiveObjectWithData:entry];
// add the object to the entries
[holderTwo addObject:entry];
}
NSLog(@"HOLDER ONE IS AFTER %@",holderOne);
NSLog(@"HOLDER TWO IS AFTER %@",holderTwo);
// assign values
peopleEntries = [NSMutableArray arrayWithArray:holderTwo];
NSLog(@"DICTIONARY IS AFTER ADDING %@",dictionaryTemp);
recordId = [NSNumber numberWithInt:[[dictionaryTemp objectForKey:@"recordId"]integerValue]];
NSLog(@"recordId is %@",recordId);
NSLog(@"LOAD RUN");
}
行,所以我返工我的代碼,但在設置斷點保存中的nskeyarchiver的結果是:[0] =(Class)所以它看起來沒有被編碼,我知道save函數可以正常工作,因爲從字典中刪除newEntries會導致recordId正確保存。任何幫助真的會被讚賞,我在這裏掙扎。 –
SmokersCough
2013-03-27 22:15:09
沒有人咬住這個?任何人都可以提供任何教程這種事情? – SmokersCough 2013-03-30 14:40:46
我的自定義類正在從NSObject擴展,它看起來我的編碼和解碼是正確的,任何人都可以提出修復? – SmokersCough 2013-06-27 16:57:52