NSArray oferuje dwie metody aby dokładnie to, co chcesz: initWithContentsOfFile:
i writeToFile:atomically:
Krótki Przykładem może wyglądać następująco:
//Creating a file path under iOS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];
//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
//Array file didn't exist... create a new one
yourArray = [[NSMutableArray alloc] initWithCapacity:10];
//Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];
Dzięki! Jedno pytanie "writeToFile:" utworzy nowy plik z tablicą w Specified Path? – Joshua
Tak. Jedno zastrzeżenie: Tablica musi być poprawną listą właściwości, co oznacza, że cała jej zawartość (i jej zawartość, w stosownych przypadkach) również muszą być listami właściwości. –
Rozumiem, że powinno być dobrze. Tak więc, kiedy ładuję tablicę z 'initWithContentsOfFile:' czy muszę podać ścieżkę bezpośrednio do pliku, a jeśli tak, to w jaki sposób będzie on wywoływany? – Joshua