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
474 views
in Technique[技术] by (71.8m points)

objective c - iOS: store two NSMutableArray in a .plist file

I want to store two NSMutableArray that I use as global array in AppDelegate. These two array are also store with NSUserDefaults. Now I want to know how I must create this file and how can I store these two array everytime I modify them. Can You help me?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
  1. Create an NSArray containing your two NSMutableArrays.

    NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil];
    
  2. Write the array to a file.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *location = [libraryDirectory stringByAppendingString:@"/somefilename.plist"];
    [array writeToFile:location atomically:YES];
    
  3. Load the array from the file.

    NSString *path = [bundle pathForResource:@"file" ofType:@"plist"];
    NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil);
    

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

...