在Cocoa中保存XML的属性列表文件(plist)是很容易的事情。NSArray,NSDictionary, NSString, 或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。

下面是保存的方法:

  1. @interface ClientDisplayMgr {
  2.    …
  3.    IBOutlet id m_clientName;   // outlets to text boxes in the preferences
  4.    IBOutlet id m_serverName;   // window.
  5.    NSArray *m_availableFriends;
  6.    …
  7. }
  8. @end
  9.  
  10. //
  11. // Save some various preferences
  12. - writePrefs
  13. {
  14.     NSMutableDictionary * prefs;
  15.  
  16.    // allocate an NSMutableDictionary to hold our preference data
  17.     prefs = [[NSMutableDictionary alloc] init];
  18.  
  19.    // our preference data is our client name, hostname, and buddy list
  20.     [prefs setObject:[m_clientName stringValue] forKey:@"Client"];
  21.     [prefs setObject:[m_serverName stringValue] forKey:@"Server"];
  22.     [prefs setObject:m_friends forKey:@"Friends"];
  23.    
  24.     // save our buddy list to the user's home directory/Library/Preferences.
  25.     [prefs writeToFile:[@"~/Library/Preferences/MiniMessage Client.plist"
  26.                     stringByExpandingTildeInPath] atomically: TRUE];
  27.     return self;
  28. }

 

保存下来的结果看起来是这样的:

  1. < ?xml version="1.0" encoding="UTF-8"?>
  2. < !DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
  3. <plist version="0.9">
  4. <dict>
  5.    <key>Client</key>
  6.    <string>Crazy Joe</string>
  7.    <key>Friends</key>
  8.    <array>
  9.       <string>Crazy Joe</string>
  10.       <string>Jim</string>
  11.       <string>Joe</string>
  12.       <string>Crazy Jim</string>
  13.       <string>Jose</string>
  14.       <string>Crazy Joe</string>
  15.    </array>
  16.    <key>Server</key>
  17.    <string>localhost</string>
  18. </dict>
  19. </plist>

要想把保存的列表文件读取出来也很简单: 

  1. - awakeFromNib
  2. {
  3.     NSString *clientName, *serverName;
  4.     NSDictionary *prefs;
  5.    
  6.     // load the preferences dictionary
  7.     prefs = [NSDictionary dictionaryWithContentsOfFile:
  8.             [@"~/Library/Preferences/MiniMessage Client.plist"
  9.                stringByExpandingTildeInPath]];
  10.    
  11.    // if the file was there, we got all the information we need.
  12.    // (note that it's probably a good idea to individually verify objects
  13.    //  we pull out of the dictionary, but this is example code :-)
  14.     if (prefs) {
  15.        //
  16.        // write our loaded names into the preference dialog's text boxes.
  17.       [m_clientName setStringValue: [prefs objectForKey:@"Client"]];
  18.       [m_serverName setStringValue: [prefs objectForKey:@"Server"]];
  19.      
  20.       //
  21.       // load our friend list.
  22.       m_friends = [[prefs objectForKey:@"Friends"] retain];
  23.     } else {
  24.        //
  25.        // no property list.  The nib file's got defaults for the
  26.        // preference dialog box, but we still need a list of friends.
  27.       m_friends = [[NSMutableArray alloc] init];
  28.       // we're our only friend (isn't it strange talking about we in the singular?)
  29.       [m_friends addObject: [m_clientName stringValue]];
  30.     }
  31.    
  32.     //
  33.     // get our preference data for the rest of awakeFromNib
  34.     clientName = [m_clientName stringValue];
  35.     serverName = [m_serverName stringValue];
  36.     …
  37. }