NSArray fornisce due metodi per fare esattamente quello che vuoi: initWithContentsOfFile:
e writeToFile:atomically:
Un breve esempio potrebbe essere simile a questo:
//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];
Grazie! Una domanda sarà 'writeToFile:' creare un nuovo file con l'array in sul percorso specificato? – Joshua
Sì. Un avvertimento: la matrice deve essere un elenco di proprietà valido, il che significa che tutti i suoi contenuti (e il loro contenuto, se del caso) devono anche essere elenchi di proprietà. –
Vedo, dovrebbe essere ok. Quindi, quando carico la matrice con 'initWithContentsOfFile:' devo dare il percorso direttamente al file e, in caso affermativo, come sarà chiamato? – Joshua