Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

objective c - How to Save NSMutableArray into plist in iphone

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

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

but it shows 0 element in my plist path. please any help me.

Thanks in advance:

following is my code to store the data into plist and NSUserDefault. none of them is working for NSMutableArray/NSArray but working for NSString. IS there any max size limit to store in plist or UserDefault?? NSMutableArray contains only text/ set of NSDictionary.

please suggest me.

- (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"];    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...