How to Save NSMutableArray into plist in iphone

后端 未结 4 1776
执笔经年
执笔经年 2020-12-10 18:42

I am new in iphone, i want to save NSMutableArray data into plist file my Code is:

NSArray *array = [[NSArray alloc] initWithArray:self.artistDetailsArray];
         


        
相关标签:
4条回答
  • 2020-12-10 19:20
    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

    0 讨论(0)
  • 2020-12-10 19:21

    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

    0 讨论(0)
  • 2020-12-10 19:35

    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]
    
    0 讨论(0)
  • 2020-12-10 19:37
    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); }
            }
    
    0 讨论(0)
提交回复
热议问题