将NSMutableArray保存到iPhone文档目录

时间:2012-04-22 15:32:20

标签: iphone objective-c nsmutablearray data-storage writetofile

我正在尝试使用名为Dog的自定义类的对象创建一个可变数组,并将其保存到iPhone文档目录中的文件中,以便稍后从文件中读出并返回到我的应用程序中。我试图使用NSArray的writeToFile:atomically:方法来实现这一点,但是当我测试此方法的结果时,它总是返回NO值,并且不创建文件,并且不存储该数组。我有几个问题。我应该将数组保存到哪种文件格式?原子地将数组写入文件意味着什么?一旦数组存储在那里,我如何读出文件的内容最重要的是,为什么我的数组没有存储在指定路径的文件中?提前谢谢,这是我在我的应用程序的viewDidLoad方法中使用的代码:

NSString *documentsDir = [NSSearchPathForDirectoriesInDomains
                          (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
dogFilePath = [documentsDir stringByAppendingPathComponent:@"arrayDogsFile.plist"];
NSFileManager *fileManager = [[NSFileManager alloc] init];

NSLog(@"%@",dogFilePath);

Dog *dog1 = [[Dog alloc] init];
dog1.name = @"Dog1";
Dog *dog2 = [[Dog alloc] init];
dog2.name = @"Dog2";
Dog *dog3 = [[Dog alloc] init];
dog3.name = @"Dog3";

NSMutableArray *arrayDogs = [NSMutableArray array];
[arrayDogs addObject: dog1];
[arrayDogs addObject: dog2];
[arrayDogs addObject: dog3];

//Sorts the array in alphabetical order according to name – compareDogNames: is defined in the Dog class
arrayDogs = (NSMutableArray *)[arrayDogs sortedArrayUsingSelector:@selector(compareDogNames:)];


if ([arrayDogs writeToFile:dogFilePath atomically:YES])
    NSLog(@"Data writing successful");
else
    NSLog(@"Data writing unsuccessful");

3 个答案:

答案 0 :(得分:2)

您无法保存对象数组,因为对象不是NSString,NSData,NSArray或NSDictionary。您更愿意使用NSKeyArchiver and NSKeyUnArchiver
例如:
#import "Foundation/Foundation.h"

@interface Dog : NSObject {**NSCoding**}//your class must conform to NSCoding Protocol  
@property (retain) NSString *Name;  
@end

实现需要一些额外的代码。我们需要实现NSCoding协议,这意味着 另外两种方法。 (initWithCoder:和encodeWithCoder:)
#import "Dog.h"

@implementation Dog 
@synthesize Name;  
-(id)initWithCoder:(NSCoder*)decoder{    
    if ((self = [super init])) {  
        Name = [decoder decodeObjectForKey:@"Name"];  
}  
    return self;  
}



-(void)encodeWithCoder:(NSCoder*)encoder{  
    [encoder encodeObject:Name forKey:@"Name"];  
} 

一旦我们实施协议,保存将如下所示:
    //保存方法
//我们初始化对象并设置值

Dog *dog1 = [[Dog alloc] init];  
dog1.Name= @"Dog1";  
Dog *dog2 = [[Dog alloc] init];  
dog2.Name= @"Dog2";  
Dog *dog3 = [[Dog alloc] init];  
dog3.Name= @"Dog3";    
NSMutableArray *arrayDogs = [NSMutableArray array];  
[arrayDogs addObject: dog1];  
[arrayDogs addObject: dog2];  
[arrayDogs addObject: dog3];  

//根据名称按字母顺序对数组进行排序 - compareDogNames:在Dog类中定义

arrayDogs = (NSMutableArray *)[arrayDogs sortedArrayUsingSelector:@selector(compareDogNames:)];  

//存储数组

[NSKeyedArchiver archiveRootObject:arrayDogs toFile:dogFilePath];  

//加载数组*

NSMutableArray* retreivedADogObjs = [NSKeyedUnarchiver unarchiveObjectWithFile:dogFilePath]; 
@end  

希望它会对你有所帮助 很乐意提供帮助。*

答案 1 :(得分:1)

看看NSKeyedArchiver& NSKeyedUnarchiver具体而言,您希望+ archiveRootObject:toFile:保存文件,+ unarchiveObjectWithFile:再次提取文件。

您需要在Dog课程中实施NSCoding协议才能使其正常工作。您只需为每个属性使用- encodeObject: forKey:– decodeObjectForKey:之类的内容。 NSCoder的文档将向您显示使用哪种属性类型的方法(例如,使用BOOL的{​​{1}})。

答案 2 :(得分:0)

工业答案: SDK中有一个完整的主题称为“Archiving and Serialization”。

如果你没有时间学习,但是你的狗会这样做: 教你的狗两个新技巧:1。如何将自己渲染为字符串和整数等词典。 2.如何从同一种字典中创建自己。这基本上是工业答案的贫民窟版本。

// Dog.m
- (NSDictionary *)asDictionary {

    NSMutableDictionary *answer = [NSMutableDictionary dictionary];
    [answer setValue:self.name forKey:@"name"];
    [answer setValue:self.numberOfBones forKey:@"numberOfBones"];
    return [NSDictionary dictionaryWithDictionary:answer];
}

- (id)initWithDictionary:(NSDictionary *)dictionary {

    self = [super init];
    if (self) {
        self.name = [dictionary valueForKey:@"name"];
        self.numberOfBones = [dictionary valueForKey:@"numberOfBones"];
    }
    return self;
}

写作时:

[arrayDogs addObject: [dog1 asDictionary]];