1
我是新手。一直試圖保存/加載一些數組,但無濟於事。 我有「Global.h」和「Global.m」爲其他類提供數組。簡單保存/加載整個陣列
在Global.h
extern NSMutableArray *arrayTable;
extern NSMutableArray *arrayPurpose;
@interface Global : NSObject <NSCoding> {
}
在Global.m
NSMutable *arrayTable;
MSMutable *arrayPurpose;
我還有其他的意見/控制器/班/任何與這些陣列的工作,他們正在運作。我在這個「全局」和「AppDelegate.h」和「AppDelegate.m」中放入了什麼,這樣當這個應用進入後臺並在應用啓動時加載這些數組時,這些數組會保存下來?我需要使用這個「NSDocumentDirectory」,因爲這個數據很重要。
請保持您的解釋非常簡單。我有不到一週的經驗。謝謝!!
編輯:沒有Joris建議。在你的appdelegate的暫停方法
你可以使用: 已將此添加到AppDelegate中
-(NSString *)archivePath {
NSString *docDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex: 0];
return [docDir stringByAppendingPathComponent:@"TableData.dat"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSKeyedArchiver archiveRootObject:arrayTable toFile:self.archivePath];
[NSKeyedArchiver archiveRootObject:arrayPurpose toFile:self.archivePath];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:rootController.view];
[self.window makeKeyAndVisible];
arrayTable = [NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]];
arrayPurpose = [NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]];
if (arrayTable == nil) {
arrayPurpose = [[NSMutableArray alloc]init];
arrayTable = [[NSMutableArray alloc]init];
}
return YES;
}
你能告訴我整個事情嗎?它是如何完成的?我一直在嘗試這些NSKeyedArchiver和NSKeyedUnarchiver幾天,並且將我的Mac從我的窗口中移出。 – 2011-06-03 15:18:53
整件事情?唯一缺少的東西是:appDelegate中的確切方法名稱,但這些應該很簡單,並且生成輸出文件路徑 – 2011-06-03 15:19:45
按照您的建議操作。聲明「archivePath」以供使用。然後是NSKeyedArchiver和Unarchiver。仍然不起作用。任何想法爲什麼?哦,爲什麼非法存檔者不允許我添加一個。自我和檔案路徑之間?它不會讓我建立。 – 2011-06-03 15:41:19