使用Cocoa保存XML格式记录文件是本文要介绍的内容,在Cocoa中保存XML的属性列表文件(plist)是很容易的事情。NSArray,NSDictionary,NSString,或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。
下面是保存的方法:
- @interfaceClientDisplayMgr{
- …
- IBOutletidm_clientName;//outletstotextBoxesinthepreferences
- IBOutletidm_serverName;//window.
- NSArray*m_availableFriends;
- …
- }
- @end
- //
- //SavesomevarIoUspreferences
- -writePrefs
- {
- NSMutableDictionary*prefs;
- //allocateanNSMutableDictionarytoholdourpreferencedata
- prefs=[[NSMutableDictionaryalloc]init];
- //ourpreferencedataisourclientname,hostname,andbuddylist
- [prefssetObject:[m_clientNamestringValue]forKey:@"Client"];
- [prefssetObject:[m_serverNamestringValue]forKey:@"Server"];
- [prefssetObject:m_friendsforKey:@"Friends"];
- //saveourbuddylisttotheuser'shomedirectory/Library/Preferences.
- [prefswriteToFile:[@"~/Library/Preferences/MiniMessageClient.plist"
- stringByExpandingTildeInPath]atomically:TRUE];
- returnself;
- }
保存下来的结果看起来是这样的:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEplistSYSTEM"file://localhost/System/Library/DTDs/PropertyList.dtd">
- <plistversion="0.9">
- <dict>
- <key>Client</key>
- <string>CrazyJoe</string>
- <key>Friends</key>
- <array>
- <string>CrazyJoe</string>
- <string>Jim</string>
- <string>Joe</string>
- <string>CrazyJim</string>
- <string>Jose</string>
- <string>CrazyJoe</string>
- </array>
- <key>Server</key>
- <string>localhost</string>
- </dict>
- </plist>
要想把保存的列表文件读取出来也很简单:
- -awakeFromNib
- {
- NSString*clientName,*serverName;
- NSDictionary*prefs;
- //loadthepreferencesdictionary
- prefs=[NSDictionarydictionaryWithContentsOfFile:
- [@"~/Library/Preferences/MiniMessageClient.plist"
- stringByExpandingTildeInPath]];
- //ifthefilewasthere,wegotalltheinformationweneed.
- //(notethatit'sprobablyagoodideatoindividuallyverifyobjects
- //wepulloutofthedictionary,butthisisexamplecode
- if(prefs){
- //
- //writeourloadednamesintothepreferencedialog'stextBoxes.
- [m_clientNamesetStringValue:[prefsobjectForKey:@"Client"]];
- [m_serverNamesetStringValue:[prefsobjectForKey:@"Server"]];
- //
- //loadourfriendlist.
- m_friends=[[prefsobjectForKey:@"Friends"]retain];
- }else{
- //
- //nopropertylist.Thenibfile'sgotdefaultsforthe
- //preferencedialogBox,butwestillneedalistoffriends.
- m_friends=[[NSMutableArrayalloc]init];
- //we'reouronlyfriend(isn'titstrangetalkingaboutweinthesingular?)
- [m_friendsaddObject:[m_clientNamestringValue]];
- }
- //
- //getourpreferencedatafortherestofawakeFromNib
- clientName=[m_clientNamestringValue];
- serverName=[m_serverNamestringValue];
- …
//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_904_403@ 原文链接:https://www.f2er.com/xml/298308.html