2012-04-22 227 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:)。