簡體   English   中英

如何將NSMutableArray保存到iphone中的plist中

[英]How to Save NSMutableArray into plist in iphone

我是iphone的新手,我想將NSMutableArray數據保存到plist文件中,我的代碼是:

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

但它在我的plist路徑中顯示0個元素。 請任何幫助我。

提前致謝:

以下是我將數據存儲到plist和NSUserDefault的代碼。 它們都不適用於NSMutableArray / NSArray,但適用於NSString。 是否存在plist或UserDefault中存儲的最大大小限制? NSMutableArray僅包含NSDictionary的文本/集。

請建議我。

- (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"];    
}
NSMutableArray *array=[[NSMutableArray alloc] init];
    [array addObject:@"test"];
    [array writeToFile:@"/Users/parag/test.plist" atomically:YES];
    [array release];

要么

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];

請以編程方式查看創建屬性列表

查看此代碼,該代碼在文檔目錄中創建plist的路徑:

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)創建路徑列表。

2)從列表中獲取文檔目錄的路徑。

3)創建完整的文件路徑。

4)檢查文件是否存在。

5)獲取之前在bundle目錄中創建的plist的路徑(通過Xcode)。

6)將此plist復制到您的文檔目錄。

下次讀取數據:

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

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

[savedStock release];

寫數據:

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]

您可以使用屬性列表(NSPropertyListSerialization或writeToFile:way)。 但請確保您的數組僅包含有效的屬性列表對象(NSString,NSNumber,NSData,NSArray或NSDictionary對象),並且NSDictionary僅包含NSString鍵。 自定義(復雜)對象必須表示為字典。

或者您應該通過NSCoding協議使用存檔http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Archiving.html 好指南在這里http://cocoadevcentral.com/articles/000084.php

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); }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM