Быстрый и грязный способ - использовать NSKeyedArchiver для записи NSArray в файл. Затем вам нужно будет прочитать данные из файла обратно в NSArray при запуске приложения.
Вот фрагмент:
Получает путь к документу:
- (NSString*)pathForDataFile
{
//Get the path of the Documents directory
NSArray* paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString* documentsDirectory = [paths objectAtIndex: 0]; //Path to the documents directory
//Get the path to my file in /Documents
NSString* documentsPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", @"FileName"]];
return documentsPath;
}
Это сохранит и загрузит:
- (BOOL)saveDataToDisk
{
NSString* path = [self pathForDataFile];
NSMutableDictionary* rootObject = [NSMutableDictionary dictionary];
if ( self.yourArray )
[rootObject setValue: self.powerListCollection.items forKey: kYourKey];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
return YES;
}
-
- (BOOL)loadDataFromDisk
{
NSString* path = [self pathForDataFile];
NSFileManager* manager = [NSFileManager defaultManager];
if ( [manager fileExistsAtPath: path] )
{
NSLog( @"Saved data found, loading data from %@", path );
NSMutableDictionary* rootObject = nil;
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile: path];
NSArray* self.yourArray = [rootObject objectForKey: kYourKey];
}
else
{
NSLog( @"No saved data, initializing objects with default values." );
}
return YES;
}
Обратите внимание, что вам нужно сохранить все, что вы получите от objectForKey: