Iphone 将项添加到NSMutableArray并保存/加载

Iphone 将项添加到NSMutableArray并保存/加载,iphone,uitableview,load,nsmutablearray,save,Iphone,Uitableview,Load,Nsmutablearray,Save,我使用教程创建了一个应用程序,其中包含一个使用NSMutableArray填充的表视图。现在我想添加功能,向数组中添加其他项并保存/加载它们。我定制了水果类,如下所示: #import <UIKit/UIKit.h> @interface Fruit : NSObject { NSString *name; NSString *instructions; NSString *explination; NSString *imagePath; } @property(nonatom

我使用教程创建了一个应用程序,其中包含一个使用NSMutableArray填充的表视图。现在我想添加功能,向数组中添加其他项并保存/加载它们。我定制了水果类,如下所示:

#import <UIKit/UIKit.h>

@interface Fruit : NSObject {
NSString *name;
NSString *instructions;
NSString *explination;
NSString *imagePath;
}

@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *instructions;
@property(nonatomic,copy) NSString *explination;
@property(nonatomic,copy) NSString *imagePath;

- (id)initWithName:(NSString*)n instructions:(NSString *)inst explination:(NSString *)why imagePath:(NSString *)img;

@end

这很好,我可以加载两个文本视图和一个图像视图,而不是一个文本视图。但我如何保存用户创建的任何新项目,并在应用程序再次启动时加载它们(如果存在)?

您需要将阵列保存在磁盘上,并在应用程序启动时加载


请参阅问题的答案。

您应该阅读的有关归档的内容:

您需要为水果类实现2种方法:

EncodeWithEncoder
InitWithEncoder

这样你就可以存档你的水果阵列了。
祝你好运。

要将阵列保存到磁盘,需要做几件事

首先,您需要向水果类添加一些方法,使其符合协议

第一种方法是
-(id)initWithCoder:(NSCoder*)aDecoder
。当您从保存的存档创建水果对象时,将调用此方法。
第二种方法是
-(void)encodeWithCoder:(NSCoder*)aCoder
。此方法用于将水果保存在存档中

听起来很复杂?事实并非如此。只有几行简单易懂的代码

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.instructions = [aDecoder decodeObjectForKey:@"instructions"];
        self.explanation = [aDecoder decodeObjectForKey:@"explanation"];
        self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"];
    }
    return self;
}
看看这个init方法的前两行。您必须调用
[super init]
并检查
self
initWithName:instructions:explation:imagePath:
方法中是否为nil。在这种特殊情况下,它不会改变任何东西,但在接下来编写的几个类中,它肯定会改变。所以要一直使用它。
我给你换了这个。我改变了拼写错误

- (id)initWithName: (NSString*)n instructions:(NSString*)inst explination:(NSString *)why imagePath:(NSString *)img {
    self = [super init];
    if (self) {
        self.name = n;
        self.instructions = inst;
        self.explanation = why;
        self.imagePath = img;
    }
    return self;
}
以及用于编码的方法:

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeObject:instructions forKey:@"instructions"];
    [aCoder encodeObject:explanation forKey:@"explanation"];
    [aCoder encodeObject:imagePath forKey:@"imagePath"];
}
键名不必与变量名匹配。你不需要这样做。但在我看来,这增加了一些清晰度。只要您使用与编码相同的名称进行解码,您就可以使用任何您想要的名称


第一部分完成了。接下来,需要加载NSMutableArray并将其保存到文件中。但要做到这一点,您需要文档目录的路径。所以我创建了一个小的helper方法,它进入到你的控制器中

- (NSString *)applicationDocumentsPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
然后我们需要从磁盘加载阵列

NSString *path = [[self applicationDocumentsPath] stringByAppendingPathComponent:@"some.fruits"];

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!array) {
    // if it couldn't be loaded from disk create a new one
    array = [NSMutableArray array];
}
然后添加任意数量的水果,最后,为了将阵列保存到磁盘,您需要这一行

BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];
如果存档没有错误,您可以检查结果



我想这应该让你开始。快乐编码

多谢各位!我现在不在电脑前,但我会尽快测试。
BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];