2012-02-20 12 views
5

Jestem nowy w iPhone, chcę zapisać dane NSMutableArray do plist złożyć mój kod to:Jak zaoszczędzić NSMutableArray do plist w iPhone

NSArray *array = [[NSArray alloc] initWithArray:self.artistDetailsArray]; 
[self.artistDetailsArray writeToFile:self.path atomically:YES]; 

ale pokazuje 0 element w mojej ścieżce plist. proszę, pomóż mi.

góry dzięki:

Następujące jest moje kodu do przechowywania danych w plist i NSUserDefault. żadna z nich nie działa dla NSMutableArray/NSArray, ale pracuje dla NSString. Czy istnieje maksymalny limit rozmiaru do przechowywania w plist lub UserDefault? NSMutableArray zawiera tylko tekst/zestaw NSDictionary.

Proszę zasugerować mi.

- (void)initialiseDataFromLocalStorage 
{ 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     self.path = [documentsDirectory stringByAppendingPathComponent:@"saveLogin.plist"]; 
     if ([fileManager fileExistsAtPath:self.path] == NO) { 
       NSString *pathToDefaultPlist = [[NSBundle mainBundle] pathForResource:@"saveLogin" ofType:@"plist"]; 
       if ([fileManager copyItemAtPath:pathToDefaultPlist toPath:self.path error:&error] == NO) { 
         NSAssert1(0,@"Failed with error '%@'.", [error localizedDescription]); 
       } 
     } 

    // To get stored value from .plist 
     NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:self.path]; 
     self.ResumedataDictionary = [[NSMutableDictionary alloc]initWithDictionary:dict]; 
     [dict release]; 
    self.artistDetailsArray = [[NSMutableArray alloc] initWithArray:[self.ResumedataDictionary objectForKey:@"artistDetailsDict"]];  
    // To get stored value from NSUserDefault 
    NSUserDefaults *fetchData = [NSUserDefaults standardUserDefaults]; 
    self.artistDetailsArray = [fetchData objectForKey:@"artistDetailsDict"];  
}   

-(void)saveDataToLocalStorage 
{  
    // To store value into .plist 
    NSArray *array = [[NSArray alloc] initWithArray:self.artistDetailsArray]; 
    [ResumedataDictionary setObject:array forKey:@"artistDetailsDict"]; 
     [ResumedataDictionary writeToFile:self.path atomically:YES]; 

    // To store value into NSUserDefault 
    NSUserDefaults *fetchData = [NSUserDefaults standardUserDefaults]; 
    [fetchData setObject:self.artistDetailsArray forKey:@"artistDetailsDict"];  
} 

Odpowiedz

0
NSData *serializedData; 
     NSString *error; 
     serializedData = [NSPropertyListSerialization dataFromPropertyList:YourArray(You can use dictionaries.strings..and others too) 
     format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 
     if (serializedData) { 
     // Serialization was successful, write the data to the file system // Get an array of paths. 

     NSArray *documentDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *docDir = [NSString stringWithFormat:@”%@/serialized.xml”, 
     [documentDirectoryPath objectAtIndex:0]]; 
     [serializedData writeToFile:docDir atomically:YES]; 
} 
     else { 
     // An error has occurred, log it 
     NSLog(@”Error: %@”,error); } 
     } 
4

spojrzenie na ten kod, który tworzy ścieżkę do PLIST w katalogu dokumentów:

NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath: path]) //4 
{ 
NSString *bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”]; //5 

[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
} 

1) Tworzenie listy ścieżek.

2) Uzyskaj ścieżkę do katalogu dokumentów z listy.

3) Utwórz pełną ścieżkę pliku.

4) Sprawdź, czy plik istnieje.

5) Uzyskaj ścieżkę do pliku plist utworzonego wcześniej w katalogu bundle (przez Xcode).

6) Skopiuj ten plik plist do katalogu z dokumentami.

obok odczytu danych:

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

//load from savedStock example int value 
int value; 
value = [[savedStock objectForKey:@"value"] intValue]; 

[savedStock release]; 

zapisu danych:

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

//here add elements to data file and write data to file 
int value = 5; 

[data setObject:[NSNumber numberWithInt:value] forKey:@”value”]; 

[data writeToFile: path atomically:YES]; 
[data release] 
+1

Witam Neon, Dzięki za szybką odpowiedź. Użyłem tego samego kodu i działa on dla wartości NSString, ale nie dla NSMutableArray lub NSArray. Wszelkie sugestie ??? :( – iPhone4s

5
NSMutableArray *array=[[NSMutableArray alloc] init]; 
    [array addObject:@"test"]; 
    [array writeToFile:@"/Users/parag/test.plist" atomically:YES]; 
    [array release]; 

lub

NSMutableArray *array=[[NSMutableArray alloc] init]; 
[array addObject:@"test121"]; 
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)array       format:NSPropertyListXMLFormat_v1_0 errorDescription:@error]; 


[plist writeToFile:@"/Users/parag/test.plist" atomically:YES]; 

Spójrz na Creating Property Lists Programmatically

3

Można używać list właściwości (NSPropertyListSerialization lub writeToFile: way). Ale upewnij się, że twoja tablica zawiera tylko poprawne obiekty listy właściwości (obiekty NSString, NSNumber, NSData, NSArray lub NSDictionary), a NSDictionary ma tylko klucze NSString. Niestandardowe (złożone) obiekty muszą być reprezentowane jako słowniki.

Lub użyj podejścia z archiwami http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Archiving.html poprzez protokół NSCoding. Ładny przewodnik jest tutaj http://cocoadevcentral.com/articles/000084.php

Powiązane problemy