以前有人问过这个问题,人们就如何做到这一点给出了很好的指导,例如这里。
但是,我想知道如果我只是想将一个 NSMutableArray(包含另一个 NSMutableArray 的各种实例)保存到文件中,是否真的需要使用 NSCoder?我试过了,但只收到一条错误消息:
-(void)saveLibraryDat {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myLibrary.dat"];
NSError *error;
[myLibrary writeToFile:filePath atomically:YES];
if (error) {
NSLog(@"There was an error saving myLibrary.dat: %@", error);
}
}
我的错误信息:
2011-05-13 22:00:47.840 MoleNotes[15437:207] There was an error saving myLibrary.dat: (
1,
2
)
所以我想我必须使用 NSCoder,对吧?如果是这样,我想知道如何去做。人们已经解释了如何用一个类来做到这一点,但就我而言,我有一个 NSMutableArray (myLibrary),它包含一个类的各种实例。我必须在这个类和 NSMutableArray 中实现 NSCoder 吗?
我像这样分配我的图书馆:
myLibrary = [[NSMutableArray alloc] init];
然后像这样添加一个名为 NoteBook.m 的类的实例:
NoteBook *newNoteBook = [[NoteBook alloc] init];
newNoteBook.titleName = @"Notes"; // etc.
[myLibrary addObject:newNoteBook];
那么我到底应该把 NSCoder 命令放在哪里呢?只进入我的 NoteBook.m 课程?这会自动处理 myLibrary 吗?
感谢您的任何建议。
编辑:
所以我更新了我的代码,但我想最大的问题是我的 NSMutableArray myLibrary 包含我设置的自定义类的几个实例(称为笔记本)。我已经为这个类(及其所有变量)设置了 NSCoding,以便我可以保存并加载它。
现在,如果我在应用程序中创建 NSMutableArray(即当应用程序第一次启动时,不存在文件),而不是从磁盘加载它,我的应用程序可以正常工作:
-(void) setupLibrary {
myLibrary = [[NSMutableArray alloc] init];
NoteBook *newNoteBook = [[NoteBook alloc] init];
newNoteBook.titleName = @"Notes";
/...
如果我从磁盘加载它,它也可以正常工作:
-(void)loadLibraryDat {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myLibrary.dat"];
myLibrary = [[NSMutableArray alloc] init];
myLibrary = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
if (!myLibrary) {
// if it couldn't be loaded from disk create a new one
NSLog(@"myLibrary.dat empty... set up new one");
[self setupLibrary];
} else { NSLog(@"Loading myLibrary.dat successful."); }
}
如果我在加载后记录库中包含的所有内容,那么一切都很好。例如,以下工作完全正常:
NSLog(@"%@", [[self.myLibrary objectAtIndex:0] titleName]);
然而,最大的问题是,如果任何其他方法试图访问 myLibrary。例如,如果我从另一个方法调用相同的日志命令,应用程序将崩溃并且我收到以下错误消息:
[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x4b38510
2011-05-14 14:09:10.490 Notes[17091:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x4b38510'
这听起来好像 myLibrary 以某种方式被释放了,但我不明白为什么。这怎么可能发生?我觉得我在我的 NSCoding 设置中做错了什么......因为如果我只是在代码中创建 myLibrary,那么一切都会非常好。只有当我从磁盘加载它时,应用程序才会崩溃。
这是班级设置:
#import <Foundation/Foundation.h>
@interface NoteBook : NSObject <NSCoding> {
NSString *titleName;
NSString *fileName;
NSMutableArray *tabTitles;
NSMutableArray *tabColours;
NSMutableArray *tabReference;
}
@property (nonatomic, retain) NSString *titleName;
@property (nonatomic, retain) NSString *fileName;
@property (nonatomic, retain) NSMutableArray *tabTitles;
@property (nonatomic, retain) NSMutableArray *tabColours;
@property (nonatomic, retain) NSMutableArray *tabReference;
-(id)initWithCoder:(NSCoder *)aDecoder;
-(void)encodeWithCoder:(NSCoder *)aCoder;
@end
//
// NoteBook.m
#import "NoteBook.h"
@implementation NoteBook
@synthesize titleName, fileName, tabTitles, tabColours, tabReference;
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.titleName = [aDecoder decodeObjectForKey:@"titleName"];
self.fileName = [aDecoder decodeObjectForKey:@"fileName"];
self.tabTitles = [aDecoder decodeObjectForKey:@"tabTitles"];
self.tabColours = [aDecoder decodeObjectForKey:@"tabColours"];
self.tabReference = [aDecoder decodeObjectForKey:@"tabReference"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:titleName forKey:@"titleName"];
[aCoder encodeObject:fileName forKey:@"fileName"];
[aCoder encodeObject:tabTitles forKey:@"tabTitles"];
[aCoder encodeObject:tabColours forKey:@"tabColours"];
[aCoder encodeObject:tabReference forKey:@"tabReference"];
}
@end
编辑:
我想我已经解决了......我忘记了一个小“自我”......这一切都搞砸了并释放了 myLibrary:
self.myLibrary = [NSKeyedUnarchiver
unarchiveObjectWithFile:filePath];
if (self.myLibrary == nil) {
NSLog(@"myLibrary.dat empty... set up new one");
[self setupLibrary];
} else { NSLog(@"Loading myLibrary.dat successful."); }