写入NSMutableArray对象到磁盘

写入NSMutableArray对象到磁盘

本文介绍了保存/写入NSMutableArray对象到磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我认为这将工作,但现在我明白这不会因为artistCollection是一个NSMutableArray的艺术家对象。

  @interface艺术家:NSObject {
NSString * firName;
NSString * surName;我的问题是什么是最好的方式录制到磁盘我的NSMutableArray的

  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.txtatomically:YES];



EDIT



,只是我最好奇的一件事。如果Artist包含其他对象(应用程序)的NSMutableArray(softwareOwned)的额外实例变量,我将如何扩展编码来覆盖这一点?我会将NSCoding添加到应用程序对象,然后在编码Artist之前对其进行编码,或者有在艺术家中指定此方法吗?

  @interface艺人:NSObject {
NSString * firName;
NSString * surName;
NSMutableArray * softwareOwned;
}

@interface应用程序:NSObject {
NSString * appName;
NSString * appVersion;
}

非常感谢



gary

解决方案

writeToFile:atomically:对于属性列表,即只对包含NSString,NSNumber,其他集合等标准对象的集合。



要详细说明,可以使用 NSKeyedArchiver 如果集合包含的所有对象都可以归档自己。要为您的自定义类实现此操作,请使其符合 NSCoding 协议:

  @interface艺术家:NSObject< NSCoding> {
NSString * firName;
NSString * surName;
}

@end


@implementation艺人

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 {
[encoding encodeObject:firName forKey:FirstNameArchiveKey];
[encoder encodeObject:surName forKey:LastNameArchiveKey];
}

@end
  NSData * artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection]; 
[artistData writeToFile:@/ Users / Fgx / Desktop / stuffatomically:YES];


Initially I thought this was going to work, but now I understand it won't because artistCollection is an NSMutableArray of "Artist" objects.

@interface Artist : NSObject {
    NSString *firName;
    NSString *surName;
}

My question is what is the best way of recording to disk my NSMutableArray of "Artist" objects so that I can load them the next time I run my application?

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

EDIT

Many thanks, just one final thing I am curious about. If "Artist" contained an extra instance variable of NSMutableArray (softwareOwned) of further objects (Applications) how would I expand the encoding to cover this? Would I add NSCoding to the "Applications" object, then encode that before encoding "Artist" or is there a way to specify this in "Artist"?

@interface Artist : NSObject {
    NSString *firName;
    NSString *surName;
    NSMutableArray *softwareOwned;
}

@interface Application : NSObject {
    NSString *appName;
    NSString *appVersion;
}

many thanks

gary

解决方案

writeToFile:atomically: in Cocoa's collection classes only works for property lists, i.e. only for collections that contain standard objects like NSString, NSNumber, other collections, etc.

To elaborate on jdelStrother's answer, you can archive collections using NSKeyedArchiver if all objects the collection contains can archive themselves. To implement this for your custom class, make it conform to the NSCoding protocol:

@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

With this, you can encode the collection:

NSData* artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection];
[artistData writeToFile: @"/Users/Fgx/Desktop/stuff" atomically:YES];

这篇关于保存/写入NSMutableArray对象到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:56