【问题标题】:Adding items to NSMutableArray and saving/loading将项目添加到 NSMutableArray 并保存/加载
【发布时间】:2011-07-18 16:23:11
【问题描述】:

我使用this 教程创建了一个应用程序,该应用程序具有使用 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

和 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

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

【问题讨论】:

    标签: iphone uitableview load nsmutablearray save


    【解决方案1】:

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

    首先,您需要向水果类添加一些方法,使其符合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;
    }
    

    看看这个 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;
    }
    

    及编码方法:

    - (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 并将其保存到文件中。但要做到这一点,您需要文档目录的路径。所以我创建了一个小助手方法,进入你的控制器。

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

    您可以检查存档是否正确完成。


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

    【讨论】:

    • 非常感谢!我现在不在电脑旁,但我会尽快测试。
    【解决方案2】:

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

    查看this问题的答案。

    【讨论】:

      【解决方案3】:

      您应该阅读有关归档的信息:NSArchiver

      你需要为你的 Fruit 类实现 2 个方法:

      EncodeWithEncoderInitWithEncoder

      你可以归档你的水果数组。 祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多