• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: plist help?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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>)

  • Prev by Date: Re: plist help?
  • Next by Date: Re: Why is Cocoa Better?
  • Previous by thread: plist help?
  • Next by thread: RE: plist help?
  • Index(es):
    • Date
    • Thread