Objective c 是否将NSMutableArray对象保存/写入磁盘?
起初我以为这会起作用,但现在我明白了,这不会起作用,因为artistCollection是一个由“艺术家”对象组成的NSMutableArray 我的问题是,将NSMutableArray中的“艺术家”对象录制到磁盘,以便下次运行应用程序时加载它们的最佳方式是什么Objective c 是否将NSMutableArray对象保存/写入磁盘?,objective-c,cocoa,Objective C,Cocoa,起初我以为这会起作用,但现在我明白了,这不会起作用,因为artistCollection是一个由“艺术家”对象组成的NSMutableArray 我的问题是,将NSMutableArray中的“艺术家”对象录制到磁盘,以便下次运行应用程序时加载它们的最佳方式是什么 artistCollection = [[NSMutableArray alloc] init]; newArtist = [[Artist alloc] init]; [newArtist setFirName:objFirNam
artistCollection = [[NSMutableArray alloc] init];
newArtist = [[Artist alloc] init];
[newArtist setFirName:objFirName];
[newArtist setSurName:objSurName];
[artistCollection addObject:newArtist];
NSLog(@"(*) - Save All");
[artistCollection writeToFile:@"/Users/Fgx/Desktop/stuff.txt" atomically:YES];
编辑
非常感谢,最后一件事我很好奇。如果“Artister”包含更多对象(应用程序)的NSMutableArray(softwareOwned)的额外实例变量,我将如何扩展编码以涵盖这一点?我会将NSCoding添加到“Applications”对象中,然后在编码“Artist”之前对其进行编码,还是有办法在“Artist”中指定这一点
非常感谢
gary看看NSKeyedArchiver。简而言之:
NSData* artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection];
[artistData writeToFile: @"/Users/Fgx/Desktop/stuff" atomically:YES];
您需要在Artist类上实现encodeWithCoder:请参见
未归档(请参见NSKeyedUnarchiver)留给读者作为练习:)
writeToFile:atomicaly:
在Cocoa的集合类中仅适用于属性列表,即仅适用于包含标准对象(如NSString、NSNumber、其他集合等)的集合
要详细说明,如果集合包含的所有对象都可以自行存档,则可以使用NSKeyedArchiver
存档集合。要为自定义类实现此功能,请使其符合NSCoding
协议:
@interface Artist : NSObject <NSCoding> {
NSString *firName;
NSString *surName;
}
@end
@implementation Artist
static NSString *FirstNameArchiveKey = @"firstName";
static NSString *LastNameArchiveKey = @"lastName";
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self != nil) {
firName = [[decoder decodeObjectForKey:FirstNameArchiveKey] retain];
surName = [[decoder decodeObjectForKey:LastNameArchiveKey] retain];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:firName forKey:FirstNameArchiveKey];
[encoder encodeObject:surName forKey:LastNameArchiveKey];
}
@end
要回答您的编辑:只需为您的应用程序类实现NSCoding,并在Artist's encodeWithCoder:和initWithCoder:中添加行来处理可变数组的编码/解码。当要求对自身进行编码时,数组将要求应用程序对象对自身进行编码。啊,我明白了,很好,谢谢。
NSData* artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection];
[artistData writeToFile: @"/Users/Fgx/Desktop/stuff" atomically:YES];
@interface Artist : NSObject <NSCoding> {
NSString *firName;
NSString *surName;
}
@end
@implementation Artist
static NSString *FirstNameArchiveKey = @"firstName";
static NSString *LastNameArchiveKey = @"lastName";
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self != nil) {
firName = [[decoder decodeObjectForKey:FirstNameArchiveKey] retain];
surName = [[decoder decodeObjectForKey:LastNameArchiveKey] retain];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:firName forKey:FirstNameArchiveKey];
[encoder encodeObject:surName forKey:LastNameArchiveKey];
}
@end
NSData* artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection];
[artistData writeToFile: @"/Users/Fgx/Desktop/stuff" atomically:YES];