使用Cocoa保存XML格式记录文件

移动开发 iOS
本文介绍的是使用Cocoa保存XML格式记录文件,如何实现,本文很详细的解决了这个问题,那么,我们来看内容。

使用Cocoa保存XML格式记录文件是本文要介绍的内容,在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;  

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

  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.     … 

小结:使用Cocoa保存XML格式记录文件的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: Cocoa China
相关推荐

2009-06-02 08:59:00

2011-08-15 15:26:20

iPhone开发CocoaXML

2011-07-07 13:51:24

Cocoa 框架

2022-03-22 09:41:31

Java编程语言持久化

2009-04-23 13:19:21

创建XMLXML文件Javascript

2011-08-10 18:13:01

Cocoa文字字符串

2011-08-15 14:47:28

Cocoa嵌入资源文件

2011-05-11 17:48:31

CocoaiOS

2013-06-08 13:29:27

Android开发DOM读取XMLXML解析

2011-06-15 09:02:01

Qt QDomDocume XML

2011-08-15 14:27:51

CocoaRunLoop

2009-09-21 18:00:49

Hibernate X

2011-08-11 15:46:55

CocoaCocoa Touch框架

2009-12-02 14:14:06

PHP DOM-XML

2022-07-15 15:11:27

SQLite微信数据库

2009-06-10 21:51:42

JavaScript XMLFirefox

2021-09-30 07:26:15

YamlJsonXml

2011-08-10 18:37:32

CocoaMac OS X

2011-09-13 18:09:15

Eclipse And

2011-05-11 15:27:58

Windows OOPCocoa MVCCocoa
点赞
收藏

51CTO技术栈公众号