使用Cocoa保存XML格式记录文件
使用Cocoa保存XML格式记录文件是本文要介绍的内容,在Cocoa中保存XML的属性列表文件(plist)是很容易的事情。NSArray,NSDictionary, NSString, 或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。
下面是保存的方法:
@interface ClientDisplayMgr { … IBOutlet id m_clientName; // outlets to text boxes in the preferences IBOutlet id m_serverName; // window. NSArray *m_availableFriends; … } @end // // Save some various preferences - writePrefs { NSMutableDictionary * prefs; // allocate an NSMutableDictionary to hold our preference data prefs = [[NSMutableDictionary alloc] init]; // our preference data is our client name, hostname, and buddy list [prefs setObject:[m_clientName stringValue] forKey:@"Client"]; [prefs setObject:[m_serverName stringValue] forKey:@"Server"]; [prefs setObject:m_friends forKey:@"Friends"]; // save our buddy list to the user's home directory/Library/Preferences. [prefs writeToFile:[@"~/Library/Preferences/MiniMessage Client.plist" stringByExpandingTildeInPath] atomically: TRUE]; return self; }
保存下来的结果看起来是这样的:
< ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> <plist version="0.9"> <dict> <key>Client</key> <string>Crazy Joe</string> <key>Friends</key> <array> <string>Crazy Joe</string> <string>Jim</string> <string>Joe</string> <string>Crazy Jim</string> <string>Jose</string> <string>Crazy Joe</string > </array> <key>Server</key> <string>localhost</string> </dict> </plist>
要想把保存的列表文件读取出来也很简单:
相关推荐
与卿画眉共浮生 2020-10-14
xiyang 2020-08-21
baijinswpu 2020-07-29
leonranri 2020-07-26
zhongliwen 2020-07-05
麋鹿麋鹿迷了路 2020-07-05
zengyu00 2020-07-05
XGQ 2020-07-04
CoderBoy 2020-06-28
whbing 2020-06-28
绝望的乐园 2020-06-27
wellfly 2020-06-26
菇星獨行 2020-06-25
草原孤狼 2020-06-25
坚持着执着 2020-06-16
wcqwcq 2020-06-14
yuanye0 2020-06-14
zhongliwen 2020-06-13
MrFuWen 2020-06-09