• IOS 對plist文件的讀寫

    2016-07-05

    在做iOS開發時,經常用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,屬性列表文件,它是一種用來存儲串行化后的對象的文件。屬性列表文件的擴展名為.plist ,因此通常被稱為 plist文件。文件是xml格式的。

    Plist文件通常用于儲存用戶設置,也可以用于存儲捆綁的信息


    我們創建一個項目來學習plist文件的讀寫。

    1、首先新建一個項目LTReadWritePlist,項目創建好以后,系統會默認創建一個plist文件,
         我們創建的工程中,plist文件是在Supporting Files下面的LTReadWritePlist-Info.plist,打開顯示如下:


    在編輯器中顯示的形式與表格類似,另外也可以使用Source code形式打開,打開plist是xml格式的。

    2、創建plist文件,

      選中工程中的Supporting Files,右鍵點擊,選擇Add File to "工程名",
      在iOS下面的Resource中選擇Property List,創建一個school.plist的文件,
      打開新創建的文件,顯示如下:


    點擊Key 下面的Root后面的加號,添加一個數據,同時修改Type為Dictionary,然后在下面添加數據,添加完成以后數據顯示如下圖:


    3、讀取plist文件數據

    讀取代碼如下:

     /*對plist文件的讀*/     NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"schoolinfo" ofType:@"plist"];     NSMutableDictionary *data=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];     NSLog(@"%@",data);     /*讀取數據結束*/

    顯示如下:


    想schoolinfo.plist中追加數據,代碼如下:

    //在plist中追加寫入數據     [data setObject:@"20" forKey:@"count"];     [data writeToFile:plistPath atomically:YES];//保存
    追加數據以后顯示如下:


    4、創建與寫plist文件

    代碼如下(data數據接上):

     /*保存數據一*/     /*新建一個plist*/     //獲取應用程序沙盒的Documents目錄     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);     NSString *newplistPath = [paths objectAtIndex:0];     //得到完整的文件名     NSString *filename=[newplistPath stringByAppendingPathComponent:@"task.plist"];     //保存數據     [data writeToFile:filename atomically:YES];     /*保存數據一結束*/          //將保存的數據讀出來     NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];     NSLog(@"%@", data1);

    5、創建string類型plist

    代碼如下:

    /*寫入string數據*/     //獲取路徑對象     NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentpath=[path1 objectAtIndex:0];     NSString *stringplistpath=[documentpath stringByAppendingPathComponent:@"stringinfo.plist"];     NSMutableDictionary *dicplist=[NSMutableDictionary dictionary];     //設置屬性     [dicplist setObject:@"20" forKey:@"age"];     [dicplist setObject:@"male" forKey:@"sex"];     [dicplist setObject:@"sports" forKey:@"hobby"];     //寫入文件     [dicplist writeToFile:stringplistpath atomically:YES];     /*結束寫入string數據*/

    6、創建Dictionary類型plist

        /*創建并寫入Dictionary鍵值plist*/     NSArray *path2=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentpath2=[path2 objectAtIndex:0];     NSString *dicplistpath=[documentpath2 stringByAppendingPathComponent:@"dicinfo.plist"];          NSMutableDictionary *rootdicplist=[NSMutableDictionary dictionary];          //定義第一個Dictionary集合     NSMutableDictionary *child1plist=[NSMutableDictionary dictionary];     [child1plist setObject:@"25" forKey:@"age"];     [child1plist setObject:@"female" forKey:@"sex"];     [child1plist setObject:@"basketball" forKey:@"hobby"];          //添加到根集合中     [rootdicplist setObject:child1plist forKey:@"xiaohua"];          //定義第二個Dictionary集合     NSMutableDictionary *child2plist=[NSMutableDictionary dictionary];     [child2plist setObject:@"23" forKey:@"age" ];     [child2plist setObject:@"male" forKey:@"sex" ];     [child2plist setObject:@"football" forKey:@"hobby" ];          [rootdicplist setObject:child2plist forKey:@"xiaozhang"];     //寫入文件     [rootdicplist writeToFile:dicplistpath atomically:YES];     /*結束創建并寫入Dictionary鍵值plist*/      

    7、修改string類型plist

    代碼如下:

     /*對string plist文件的修改*/     NSArray *changepath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *changestringpath=[changepath objectAtIndex:0];     NSString *endchangepath=[changestringpath stringByAppendingPathComponent:@"stringinfo.plist"];     NSMutableDictionary *mutablestring=[[NSMutableDictionary alloc] initWithContentsOfFile:endchangepath];     NSString *age=[mutablestring objectForKey:@"age"];     age=@"27";     [mutablestring setObject:age forKey:@"age"];     [mutablestring writeToFile:endchangepath atomically:YES];     [mutablestring release];     /* end 對string plist文件的修改*/

    8、修改Dictionary類型plist

    /*對Dictionary plist文件的修改*/     NSString *changedicpath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dicinfo.plist"];     NSMutableDictionary *dicinfolist=[[NSMutableDictionary alloc] initWithContentsOfFile:changedicpath];     //獲取小張的信息     NSMutableDictionary *dicuser=[dicinfolist objectForKey:@"xiaozhang"];     [dicuser setObject:@"female" forKey:@"sex"];     [dicinfolist setObject:dicuser forKey:@"xiaozhang"];     [dicinfolist writeToFile:changedicpath atomically:YES];

    示例下載地址:下載




    天堂网