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

Sim*_*n M 2 iphone load save uitableview nsmutablearray

我已经使用教程创建了一个带有使用NSMutableArray填充的表视图的应用程序.现在我想添加功能,以便向阵列添加其他项目并保存/加载它们.我已经将Fruit类定制为如下所示:

#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
Run Code Online (Sandbox Code Playgroud)

和Fruit.m文件:

#import "Fruit.h"

@implementation Fruit
@synthesize name,instructions,explination,imagePath;

- (id)initWithName: (NSString*)n instructions:(NSString*)inst explination:(NSString *)why imagePath:(NSString *)img {
    self.name = n;
    self.instructions = inst;
    self.explination = why;
    self.imagePath = img;
    return self;
}
@end
Run Code Online (Sandbox Code Playgroud)

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

Mat*_*uch 8

要将阵列保存到磁盘,您需要做一些事情.

首先,您需要向fruit类添加一些方法,以使其符合NSCoding协议.

第一种方法是- (id)initWithCoder:(NSCoder *)aDecoder.从已保存的存档创建Fruit对象时,将调用此方法.
第二种方法是- (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;
}
Run Code Online (Sandbox Code Playgroud)

看一下这个init方法的前两行.[super init]如果self你的initWithName:instructions:explination:imagePath:方法也不是零,你必须打电话并检查.在这种特殊情况下它不会改变任何东西,但是在你编写的下几个类中肯定会改变.所以一直使用它.
我为你换了这个.我改变了拼写错误.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

和编码方法:

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeObject:instructions forKey:@"instructions"];
    [aCoder encodeObject:explanation forKey:@"explanation"];
    [aCoder encodeObject:imagePath forKey:@"imagePath"];
}
Run Code Online (Sandbox Code Playgroud)

密钥名称不必与变量名称匹配.你不需要这样做.但在我看来,它增加了一些清晰度.只要您使用与编码相同的名称进行解码,就可以使用您想要的任何内容.


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

- (NSString *)applicationDocumentsPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Run Code Online (Sandbox Code Playgroud)

然后我们需要从磁盘加载数组.

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];
}
Run Code Online (Sandbox Code Playgroud)

然后你可以添加尽可能多的水果,最后,为了将你的阵列保存到磁盘你需要这一行.

BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];
Run Code Online (Sandbox Code Playgroud)

如果存档完成且没有错误,您可以检查结果.


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