czy można zapisać dane NSMutableArray lub nsdictionary jako plik w iOS?
Chcę zapisać NSMutableArray
LUB NSDictionary
jako plik stały. Następnie chciałbym otworzyć plik później.
18
Author: lukasgeiter, 2010-09-30
2 answers
Do zapisu w pliku
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; [myArray writeToFile:filePath atomically:YES];
Do odczytu z pliku
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; myArray = [NSArray arrayWithContentsOfFile:filePath];
MyArray będzie zerowe, jeśli plik nie jest obecny. I NSDictionary ma podobne metody. Proszę sprawdzić odniesienie dla szczegółów tych metod.
51
Author: taskinoor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-06 12:22:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-06 12:22:43
Być może możesz przekonwertować Słownik na dane JSON.i zapisz to do pliku.
NSDictionary *dict = /* the NSDictionary/NSArray you need to write */
NSError *writeError = nil;
NSData* data;
data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&writeError];
if (!writeError) {
[data writeToFile:filePath atomically:YES];
}
1
Author: Hancock_Xu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-16 07:01:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-16 07:01:02