如何在App终止时使用NSUserDefault保存自定义对象数组?

时间:2012-02-01 23:48:24

标签: objective-c ios ios4

所以我在应用程序终止时尝试保存数组,以便在我开始备份时可以重新填充数组。我遇到了很多不同的错误,主要是当我尝试在应用程序启动时重新创建数组时。我已经采取措施对自定义对象进行编码和解码,但也许我没有正确地做到这一点......这是我的代码:

- (void)applicationWillTerminate:(UIApplication *)application
{
/*
 Called when the application is about to terminate.
 Save data if appropriate.
 See also applicationDidEnterBackground:.
 */

//Here is Where I save the array. "tasks is a pre-populated array of objects

NSData *taskData = [NSKeyedArchiver archivedDataWithRootObject:tasks];
[[NSUserDefaults standardUserDefaults]setObject:taskData forKey:@"tasks"];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

tasks = [[NSMutableArray alloc]init];

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

//NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if([[NSUserDefaults standardUserDefaults] objectForKey:@"tasks"]){

    NSData *tasksData = [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"];

 //It usually breaks right here

 tasks = [NSKeyedUnarchiver unarchiveObjectWithData:tasksData];

}

这是“任务”数组包含的“任务”对象:

Task.h

#import <Foundation/Foundation.h>

@class TeamMember;
@class Project;


@interface Task : NSObject<NSCoding>{

NSDate *endDate;
NSDate *startDate;
NSMutableString* notes;
NSMutableString* taskName;
TeamMember *teamMember;
Project *project;
}
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder; 

@property  (nonatomic, retain)TeamMember *teamMember;
@property  (nonatomic, retain) Project * project;

@property (nonatomic, retain) NSMutableString *notes;
@property (nonatomic, retain) NSMutableString *taskName;
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) NSDate *endDate;


@end

Task.m

#import "Task.h"


@implementation Task
@synthesize notes;
@synthesize taskName;
@synthesize endDate;
@synthesize startDate;
@synthesize teamMember;
@synthesize project;


//@synthesize project;
- (void)encodeWithCoder:(NSCoder *)encoder {
//Encode properties, other class variables, etc
[encoder encodeObject:self.notes forKey:@"notes"];
[encoder encodeObject:self.taskName forKey:@"taskName"];
[encoder encodeObject:self.endDate forKey:@"endDate"];
[encoder encodeObject:self.startDate forKey:@"startDate"];

//you need to add in the encoding for teammember and project
}

- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super init])) {
    //decode properties, other class vars
    self.notes = [decoder decodeObjectForKey:@"notes"];
    self.taskName = [decoder decodeObjectForKey:@"taskName"];
    self.endDate = [decoder decodeObjectForKey:@"endDate"];
    self.startDate = [decoder decodeObjectForKey:@"startDate"];

    //decode teamMember and Project here

}
}



@end

1 个答案:

答案 0 :(得分:2)

忽略将这样的数组保存到NSUserDefaults而不是使用CoreData这一事实的设计很糟糕,你的问题是你没有同步默认值。在[[NSUserDefaults standardUserDefaults] synchronize];方法的底部添加此行applicationWillTerminate

请使用CoreData。用户默认值不是用于数据持久性,它们旨在存储一些小块数据,如bool,或某些需要恢复的特定区域的宽度和高度,而不是自定义用户内容,实际上需要持续存在。

此外,请勿使用applicationWillTerminate方法来保存数据,并在用户添加后立即保存。此方法不保证其内容执行将正确完成。