2012-04-22 80 views
1

我想创建一个可变数组与一个名为狗的自定义类的对象,并将其保存到iPhone文档目录中的文件,以便以后读出该文件并返回到我的应用程序。我尝试使用NSArray的writeToFile:atomically:方法来完成此操作,但是当我测试此方法的结果时,它始终返回NO值,并且未创建该文件,并且未存储该数组。我对此有几个问题。我应该将数组保存到什么文件格式?以原子方式将数组写入文件意味着什么?一旦数组存储在那里,如何读取文件的内容?最重要的是,为什么我的数组没有存储到指定路径的文件中?谢谢你在前进,这里是我使用我的应用程序的viewDidLoad方法中的代码:保存NSMutableArray到iPhone文档目录

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"); 
+1

查找到归档/解档 - 你的狗班将不得不采用NSCoding协议。 – 2012-04-22 16:09:37

回答

2

您无法保存您的对象的数组,因为对象不是的NSString,NSData的,NSArray的,或者NSDictionary.You可能宁愿用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 

希望它会帮助你
很高兴能帮助你。*

+0

非常感谢。这种方法完美地工作 – bnasty 2012-04-23 21:33:29

0

工业答案: 有一个名为“Archiving and Serialization”的SDK为这个整体问题。

如果你没有时间去学习,但你的狗做: 教你的狗了两个新的花样: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]]; 
1

NSKeyedArchiver & NSKeyedUnarchiver具体来说,你要+ archiveRootObject:toFile:保存文件并+ unarchiveObjectWithFile:再次提取它。

您需要在您的Dog类中实施NSCoding协议才能完成此工作。你只需要为你的每个属性使用- encodeObject: forKey:– decodeObjectForKey:之类的东西。 NSCoder的文档将向您显示使用哪种方法来使用哪种类型的属性(例如,使用BOOL,您使用的是- encodeBool: forKey:)。

相关问题