开发者

Need To Make A NSMutableArray That Is Writable To Disk

开发者 https://www.devze.com 2023-02-17 21:54 出处:网络
I want to have a tab开发者_如何学Gole view that is blank at start. The user will be able to add entries and sub entries to design a workout routine.

I want to have a tab开发者_如何学Gole view that is blank at start.

The user will be able to add entries and sub entries to design a workout routine.

For example, table is blank at first, then user adds "Day 1", and adds bench press, bicep curl, etc. into this Day 1 sub table. Then he adds a Day 2, and adds some other exercises in there.

I already have all these strings that user can choose from in a plist that is loaded into a different table that just sorts these strings my muscle group. What is the best way to go about this?

I hope my question is clear if not I will try better to reword it.


I've done something similar where I actually just wrote the contents of my array out to a PLIST file and steered clear of the keyed archiver. You can't use this approach if you have custom objects in your array. I believe your array (or dictionary) contents can only be of type nsstring, nsnumber, nsdata, nsarray, nsdate and nsdictionary (there could be more?!?).

I have a singleton that controls loading and writing the data from/to local storage. It also contains the single array object that I manipulate from my various view controllers.

Within init, I load my array property (deep mutable copy is a category method I've built for nssarray):

- (void) loadYourArray {
NSArray *tempArray = [NSArray arrayWithContentsOfFile:[Utils getFileStorageLocation]];
if (tempArray == nil) {
    yourArray = [[NSMutableArray alloc] init];
} else {
    yourArray = [[NSArray deepMutableCopy:tempArray] retain];
}}

when I'm ready to save my data, I simply call a persist method:

- (void)persistYourArray:(BOOL)broadcast {
[yourArray writeToFile:[Utils getFileStorageLocation] atomically:YES];

if (broadcast) {
    // notify some peeps
}}
0

精彩评论

暂无评论...
验证码 换一张
取 消