Итак, я пытаюсь сохранить массив, когда приложение завершается, чтобы я мог заново заполнить массив при запуске резервного копирования. Я сталкиваюсь с множеством разных ошибок, в основном, когда я пытаюсь воссоздать массив при запуске приложения. Я предпринял шаги для кодирования и декодирования пользовательских объектов, но, возможно, я делаю это неправильно ... Вот мой код:
- (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