使用Cocoa保存XML格式记录文件是本文要介绍的内容,在Cocoa中保存XML的属性列表文件(plist)是很容易的事情。NSArray,NSDictionary,NSString,或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。
下面是保存的方法:
- @interfaceClientDisplayMgr{ @H_404_25@…
- IBOutletidm_clientName;//outletstotextBoxesinthepreferences @H_404_25@IBOutletidm_serverName;//window.
- NSArray*m_availableFriends; @H_404_25@…
- } @H_404_25@@end
- @H_404_25@//
- //SavesomevarIoUspreferences @H_404_25@-writePrefs
- { @H_404_25@NSMutableDictionary*prefs;
- @H_404_25@//allocateanNSMutableDictionarytoholdourpreferencedata
- prefs=[[NSMutableDictionaryalloc]init]; @H_404_25@
- //ourpreferencedataisourclientname,hostname,andbuddylist @H_404_25@[prefssetObject:[m_clientNamestringValue]forKey:@"Client"];
- [prefssetObject:[m_serverNamestringValue]forKey:@"Server"]; @H_404_25@[prefssetObject:m_friendsforKey:@"Friends"];
- @H_404_25@//saveourbuddylisttotheuser'shomedirectory/Library/Preferences.
- [prefswriteToFile:[@"~/Library/Preferences/MiniMessageClient.plist" @H_404_25@stringByExpandingTildeInPath]atomically:TRUE];
- returnself; @H_404_25@}
保存下来的结果看起来是这样的:
- <?xmlversion="1.0"encoding="UTF-8"?> @H_404_25@<!DOCTYPEplistSYSTEM"file://localhost/System/Library/DTDs/PropertyList.dtd">
- <plistversion="0.9"> @H_404_25@<dict>
- <key>Client</key> @H_404_25@<string>CrazyJoe</string>
- <key>Friends</key> @H_404_25@<array>
- <string>CrazyJoe</string> @H_404_25@<string>Jim</string>
- <string>Joe</string> @H_404_25@<string>CrazyJim</string>
- <string>Jose</string> @H_404_25@<string>CrazyJoe</string>
- </array> @H_404_25@<key>Server</key>
- <string>localhost</string> @H_404_25@</dict>
- </plist>
要想把保存的列表文件读取出来也很简单:
- -awakeFromNib @H_404_25@{
- NSString*clientName,*serverName; @H_404_25@NSDictionary*prefs;
- @H_404_25@//loadthepreferencesdictionary
- prefs=[NSDictionarydictionaryWithContentsOfFile: @H_404_25@[@"~/Library/Preferences/MiniMessageClient.plist"
- stringByExpandingTildeInPath]]; @H_404_25@
- //ifthefilewasthere,wegotalltheinformationweneed. @H_404_25@//(notethatit'sprobablyagoodideatoindividuallyverifyobjects
- //wepulloutofthedictionary,butthisisexamplecode @H_404_25@if(prefs){
- // @H_404_25@//writeourloadednamesintothepreferencedialog'stextBoxes.
- [m_clientNamesetStringValue:[prefsobjectForKey:@"Client"]]; @H_404_25@[m_serverNamesetStringValue:[prefsobjectForKey:@"Server"]];
- @H_404_25@//
- //loadourfriendlist. @H_404_25@m_friends=[[prefsobjectForKey:@"Friends"]retain];
- }else{ @H_404_25@//
- //nopropertylist.Thenibfile'sgotdefaultsforthe @H_404_25@//preferencedialogBox,butwestillneedalistoffriends.
- m_friends=[[NSMutableArrayalloc]init]; @H_404_25@//we'reouronlyfriend(isn'titstrangetalkingaboutweinthesingular?)
- [m_friendsaddObject:[m_clientNamestringValue]]; @H_404_25@}
- @H_404_25@//
- //getourpreferencedatafortherestofawakeFromNib @H_404_25@clientName=[m_clientNamestringValue];
- serverName=[m_serverNamestringValue]; @H_404_25@…
//test OK!
-(void) writePrefs_test
{
id m_clientName; // outlets to text Boxes in the preferences
id m_serverName; // window.
NSMutableDictionary * prefs;
NSString *aa=@"1243";
m_clientName = aa.copy;
NSString *aa_value;
aa_value = m_clientName;
// allocate an NSMutableDictionary to hold our preference data
prefs = [[NSMutableDictionary alloc] init];
// our preference data is our client name,and buddy list
//Save:
// [prefs setObject:m_clientName forKey:@"Client1"];
NSArray *classdata1 = [[NSArray alloc] initWithObjects:@"124c","name1",@"22",nil];
[prefs setObject:classdata1 forKey:@"ID1"];
NSArray *classdata2 = [[NSArray alloc] initWithObjects:@"124a","name2",@"11",nil];
[prefs setObject:classdata2 forKey:@"ID2"];
NSArray *classdata3 = [[NSArray alloc] initWithObjects:@"1244","name3",nil];
[prefs setObject:classdata3 forKey:@"ID3"];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documents = [path objectAtIndex:0];
NSString *filepath = [documents stringByAppendingPathComponent:@"a.plist"];
if([manager createFileAtPath:filepath contents:nil attributes:nil])
{
printf("文件创建成功");
}
else
{
printf("创建失败");
}
// save our buddy list to the user's home directory/Library/Preferences.
BOOL bSaved;
bSaved =[prefs writeToFile:[filepath
stringByExpandingTildeInPath] atomically: TRUE];
// BOOL bSaved;
// bSaved = [prefs writeToFile:[filepath
// stringByExpandingTildeInPath] atomically: YES];
if(TRUE == bSaved)
{
printf("Save ok! %d",bSaved);
}
else
{
printf("Save Failed! %d",bSaved);
}
// return self;
}
@H_533_403@