将项目添加到NSMutableArray并保存/加载 [英] Adding items to NSMutableArray and saving/loading

查看:53
本文介绍了将项目添加到NSMutableArray并保存/加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了教程来创建具有使用NSMutableArray填充的表格视图的应用程序.现在,我想添加功能,以将其他项目添加到数组并保存/加载它们.我自定义了Fruit类,如下所示:

I've used this tutorial to create an app with a table view that is populated using an NSMutableArray. Now I'd like to add the functionality to add additional items to the array and save/load them. I've customized the Fruit class to look like this:

#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

和Fruit.m文件:

and the Fruit.m file:

#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

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

and this works great, I can load two textviews and an imageView, instead of just one textview. But how would I go about saving any new items the user creates, and loading them (if they exist) when the app gets launched again?

推荐答案

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

to save your array to disk you need a couple of things.

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

first you need to add some methods to your fruit class so it conforms to the NSCoding protocol.

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

The first method is - (id)initWithCoder:(NSCoder *)aDecoder. This method will be called when you create a Fruit object from a saved archive.
Second method is - (void)encodeWithCoder:(NSCoder *)aCoder. This method is used to save your fruit in an archive.

听起来复杂吗?其实不是.仅有几行易于理解的代码.

Sounds complicated? Actually it isn't. Just a couple lines of easy to understand code.

- (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] 并在 initWithName:instructions:explination:imagePath:方法中也检查 self 是否不为零..在这种特殊情况下,它不会改变任何东西,但是在您接下来编写的几类中,这肯定会改变.因此,请一直使用它.
我替你改了而且我更改了拼写错误.

Look at first two lines of this init method. You have to call [super init] and do a check if self is not nil in your initWithName:instructions:explination:imagePath: method too. It won't change anything in this special case, but this will definitely change in the next few classes you write. So use it all the time.
I changed this for you. And I changed the spelling error.

- (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"];
}

键名不必与变量名匹配.您不需要这样做.但是我认为它增加了一些清晰度.只要使用与编码时相同的名称进行解码,就可以使用所需的任何名称.

It's not necessary that the key name matches the variable name. You don't need to do this. But in my opinion it adds some clarity. As long as you decode with the same name you've used for encoding you can use whatever you want.

第一部分完成.接下来,您需要加载NSMutableArray并将其保存到文件中.但是要做到这一点,您需要文件目录的路径.因此,我创建了一个用于您的控制器的辅助方法.

First part is done. Next you need to load and save your NSMutableArray to a file. But to do this you need the path to the documents directory. So I created a little helper method that goes into your controller.

- (NSString *)applicationDocumentsPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

然后,我们需要从磁盘加载阵列.

Then we need to load the array from disk.

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

然后,您可以添加任意数量的水果,最后,需要将该行保存到磁盘上.

then you add as much fruits as you like, and finally, to save your array to disk you need this line.

BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];

您可以检查归档是否正确完成了.

you can check result if the archive was done without error.

我想这应该可以帮助您入门.快乐的编码.

I guess this should get you started. Happy coding.

这篇关于将项目添加到NSMutableArray并保存/加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆