2011-03-19 44 views
2

我已經使用this教程來創建一個應用程序,該應用程序具有使用NSMutableArray填充的表視圖。現在我想添加功能來添加額外的項目到數組並保存/加載它們。我已經定製了水果類,看起來像這樣:將項目添加到NSMutableArray並保存/加載

#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 

來完成這項工程,我能裝載兩個textviews和ImageView的,而不是隻一個textview。但是,我將如何去保存用戶創建的任何新項目,並在app再次啓動時加載它們(如果存在)?

回答

8

要將陣列保存到磁盤,您需要一些事情。

首先你需要添加一些方法到你的水果類,以便它符合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],並在initWithName:instructions:explination:imagePath:方法中檢查self是否爲零。在這種特殊情況下,它不會改變任何東西,但是在接下來的幾節課中,這肯定會改變。所以一直使用它。
我改變了你的這個。我改變了拼寫錯誤。

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

如果歸檔沒有錯誤,您可以檢查結果。


我想這應該讓你開始。快樂的編碼。

+0

非常感謝!目前我不在我的電腦上,但我會盡快測試。 – 2011-03-19 22:25:30

+0

它工作太棒了!謝謝! – 2011-03-20 01:39:47

1

您需要將陣列保存在磁盤上並在應用程序啓動時加載它。

請參閱this問題的答案。

0

比你應該閱讀有關歸檔:NSArchiver

你需要實現你的水果類2方法:

EncodeWithEncoderInitWithEncoder

比你可以歸檔你的水果數組。 祝你好運。