Re: plist help?
Re: plist help?
- Subject: Re: plist help?
- From: Ali Ozer <email@hidden>
- Date: Sat, 20 Sep 2003 12:09:13 -0700
I've got ten or so dictionaries that I'd like to store in a single
plist file (if possible). I envision the resulting plist file looking
something like this:
<plist version="1.0">
<key>dictOne</key>
<dict>
<key>stringOneOne</key>
<string>stringOneOneData</string>
... // for size of dict = 5
<key>stringOneFive</key>
<string>stringOneFiveData</string>
</dict>
... // for number of dicts = 10
<key>dictTen</key>
<dict>
<key>stringTenOne</key>
<string>stringTenOneData</string>
... // for size of dict = 4
<key>stringTenFour</key>
<string>stringTenFourData</string>
</dict>
</plist>
However, given the methods in NSPropertyListSerialization, this sort
of plist would be very messy to write and read (if it can be done at
all). The only solution I can see is to have 10 separate plist files,
but I'd like to keep all this data in a single preferences file. Any
thoughts?
Not messy at all --- seems like your plist is just a dictionary
containing dictionaries which contain strings. Construct your data
structure starting with NSMutableDictionary:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *secondaryDict;
secondaryDict = [[NSMutableDictionary alloc] init]; // You can also
just use [NSDictionary dictionaryWithObjectsAndKeys: ...] to create the
dict in one step
[secondaryDict setObject: stringOneOneData forKey: stringOneOne];
[secondaryDict setObject: stringOneFiveData forKey: stringOneFive];
[dict setObject:secondaryDict forKey: dictOne];
[secondaryDict release];
// ... create additional secondaryDicts as needed
// and when done, create the flattened plist with
NSString *errorString;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dict
format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorString];
if (!data) {
// show user the errorString...
} else {
[data writeToFile:yourFile atomically:YES];
}
[dict release];
Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >plist help? (From: Daniel Todd Currie <email@hidden>) |