cocoa read xml and save xml

本文介绍如何在Cocoa中使用属性列表(plist)格式保存XML类型的记录文件,包括保存方法和读取过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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.     … 
My test
Save:

//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, hostname, 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",@"22",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;

}


内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值