gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:16:47 25 4
gpt4 key购买 nike

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

- (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];

}

这里是“tasks”数组包含的“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

任务.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

最佳答案

尽管将这样的数组保存到 NSUserDefaults 而不是使用 CoreData 是糟糕的设计,但您的问题是您没有同步默认值。添加此行 [[NSUserDefaults standardUserDefaults] synchronize];applicationWillTerminate 方法的底部。

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

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

关于objective-c - App终止时如何使用NSUserDefault保存自定义对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9105152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com