How to Save NSMutableArray into plist in iphone

≯℡__Kan透↙ 提交于 2019-11-28 11:32:28
NSMutableArray *array=[[NSMutableArray alloc] init];
    [array addObject:@"test"];
    [array writeToFile:@"/Users/parag/test.plist" atomically:YES];
    [array release];

or

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

Take a look at Creating Property Lists Programmatically

Nimit Parekh

look at this code which creates path to plist in documents directory:

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) Create a list of paths.

2) Get a path to your documents directory from the list.

3) Create a full file path.

4) Check if file exists.

5) Get a path to your plist created before in bundle directory (by Xcode).

6) Copy this plist to your documents directory.

next read data:

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

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

[savedStock release];

write data:

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]

You can use property lists (NSPropertyListSerialization or writeToFile: way). But be sure your array contains valid property list objects only (NSString, NSNumber, NSData, NSArray, or NSDictionary objects) and NSDictionary has only NSString keys. Custom (complex) objects have to be represented as dictionaries.

Or you should use approach with archives http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Archiving.html through NSCoding protocol. Nice guide is here 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); }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!