• IOS plist文件


    转自:http://www.cnblogs.com/geraldzhang/archive/2011/08/24/2152121.html

    Mac OS X 的Cocoa ,NeXTSTEP 和GNUstep 编程框架中, 属性列表(Property List)文件是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。

    Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息,该功能在旧式的Mac OS中是由资源分支提供的。

    使用mac os 和Core Foundation中的property list接口我们可以在层式的对象和xml文件之间进行转换。我们可以把xml文件存储起来以后再把它以对象的形式读取出来。这里我们来具体讨论下property list和他们的表现形式,以及如何在编程过程中使用他们。

    这里我想提到一下NSUserDefault,它其实也是以property list 的形式来存储的,但是它有限制,比如说NSColor和NSFont等类型式不能够直接存储的,我们必须要转换他们,要把他们转换成NSData类型来存储,我想在另一篇文章在详细说说这个问题。

    废话不多说,我们开始吧。

    在编程的过程中,我们可以在项目中建立plist来存储一些变量,具体的操作步骤File-new-Mac OS X-Resource-Property List。我们在项目中可以以xml形式或者source Code形式来编写。比如我们的plist原代码的形式象下面的xml一样。

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
    3. <plist version="1.0">  
    4. <dict>  
    5.     <key>Name</key>  
    6.     <string>John Doe</string>  
    7.     <key>Phones</key>  
    8.     <array>  
    9.         <string>408-974-0000</string>  
    10.         <string>503-333-5555</string>  
    11.     </array>  
    12. </dict>  
    13. </plist>  

    接下来我们从plist中读取信息,这是在iPhone开发中的应用:

    C代码  收藏代码
    1.  //get the plist file from bundle  
    2.  NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];   
    3. // build the array from the plist  
    4. NSMutableArray *anArray = [[NSMutableArray alloc]initWithContentOfFile:plistPath];  

    下面是写操作

    C代码  收藏代码
    1. NSString *error;  
    2.   
    3. NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   
    4.                                 NSUserDomainMask, YES) objectAtIndex:0];  
    5.   
    6. NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];  
    7.   
    8. NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:  
    9.   
    10.         [NSArray arrayWithObjects: personName, phoneNumbers, nil]  
    11.   
    12.         forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];  
    13.   
    14. NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict  
    15.   
    16.                         format:NSPropertyListXMLFormat_v1_0  
    17.   
    18.                         errorDescription:&error];  
    19.   
    20. if(plistData) {  
    21.   
    22.     [plistData writeToFile:plistPath atomically:YES];  
    23.   
    24. }  
    25.   
    26. else {  
    27.   
    28.     NSLog(error);  
    29.   
    30.     [error release];  
    31.   
    32. }  

     主要的内容来自于:

    http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html#//apple_ref/doc/uid/10000048i-CH4-SW5

  • 相关阅读:
    OpenCR 固件修复
    E-PUCK2机器人-固件更新
    E-puck2机器人系列教程-2.软件的安装与使用
    E-PUCK2机器人-硬件
    E-puck2机器人系列教程-固件修复升级
    GridView
    TimePicker 和TimePickerDiag
    android中实现简单的播放
    ListView的使用
    android的activity的跳转
  • 原文地址:https://www.cnblogs.com/LCGIS/p/3224871.html
Copyright © 2020-2023  润新知