plist的讀取和寫入(增刪改)附demo 見文章結尾

來源:互聯網
上載者:User

代碼見 http://download.csdn.net/detail/x1135768777/4197663

在.h檔案內聲明

 NSMutableDictionary* dict;

  UITextView* textView;

在.m檔案內

//

//  PlistViewController.m

//  Plist read and write

//

//  Created by wang doublejie on 12-3-31.

//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.

//

#import "PlistViewController.h"

@implementation PlistViewController

#define PATH [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]

@synthesize textView,dict;

-(void)viewDidLoad

{   

    [superviewDidLoad];  

    dict =  [[NSMutableDictionaryalloc ]initWithContentsOfFile:PATH];//檔案位置

    NSMutableDictionary* item= [dictobjectForKey:@"item1"];//第一層
NSMutableDictionary

    NSMutableDictionary* page= [itemobjectForKey:@"page1"];//第二層NSMutableDictionary

    //plist裡面想讀取的值的上面有幾個NSMutableDictionary就要幾個NSMutableDictionary來解讀

    NSString *content  = [page
objectForKey:@"content"]; //內容

    NSString *position = [page
objectForKey:@"content_position"];
//文字位置

    NSArray *arrPos =[positioncomponentsSeparatedByString:NSLocalizedString(@",",nil)];//位置數組拆分

    textView=[[UITextViewalloc]initWithFrame:CGRectMake([[arrPosobjectAtIndex:0]floatValue],
[[arrPosobjectAtIndex:1]floatValue], [[arrPosobjectAtIndex:2]floatValue], [[arrPosobjectAtIndex:3]floatValue])]

   // [arrPos objectAtIndex:3]數組中第三個數值[[arrPos objectAtIndex:1] floatValue]是將讀出的NSString轉換成NSString;

    textView.text=content;//將讀出的內容賦值給text;

   // textView.userInteractionEnabled = NO;取消文本複製

    textView.editable=NO;

    [self.viewaddSubview:textView];//添加視圖上

    NSString* item2= [dictobjectForKey:@"item1"];//讀取item2

    NSLog(@"%@",item2);

    NSString* page_num= [item
objectForKey:@"page_num"]; //讀取page_num

    NSLog(@"%@",page_num);

    NSArray* arr=[dictobjectForKey:@"array"];

    for(int i=0,j=2;i<=5;i++,j++)

    {

        //NSString* str=[[NSString alloc]initWithFormat:@"item%d",i];

        NSString* item= [arr
objectAtIndex:i];

        textView=[[UITextViewalloc]initWithFrame:CGRectMake(90, 50*j, 70, 20)];

        textView.text=item;

        textView.editable=NO;

        [self.viewaddSubview:textView];//添加視圖上

    }

    for(int count=2;count<[dictcount];count++)//count是其儲存的數目

    {

        NSString* str=[[NSStringalloc]initWithFormat:@"item%d",count];

        NSString* item= [dictobjectForKey:str];

        textView=[[UITextViewalloc]initWithFrame:CGRectMake(150, 50*count, 70, 20)];

        textView.text=item;

        textView.editable=NO;

        [self.viewaddSubview:textView];//添加到視圖上 

    }

    UIButton *addbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    addbtn.frame = CGRectMake(0, 60, 90, 35);

    [addbtn setTitle:@"添加"forState:UIControlStateNormal];

    [addbtn setTitle:@"添加"forState:UIControlStateHighlighted];

    [addbtn addTarget:selfaction:@selector(add:)forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:addbtn];//添加到視圖上 

    UIButton *editbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    editbtn.frame = CGRectMake(0, 160, 90, 35);

    [editbtn setTitle:@"修改"forState:UIControlStateNormal];

    [editbtn setTitle:@"修改"forState:UIControlStateHighlighted];

    [editbtn addTarget:selfaction:@selector(edit:)forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:editbtn];//添加到視圖上

    UIButton *delbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    delbtn.frame = CGRectMake(0, 260, 90, 35);

    [delbtn setTitle:@"刪除"forState:UIControlStateNormal];

    [delbtn setTitle:@"刪除"forState:UIControlStateHighlighted];

    [delbtn addTarget:selfaction:@selector(del:)forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:delbtn];//添加到視圖上   

}

-(IBAction)add:(id)sender 

{

    [dictsetObject:@"item8"forKey:@"item8"];//添加更修改方法相同,他是先尋找是否存在該項,如果存在修改該項,如果不存在直接添加

    [dict writeToFile:PATHatomically:YES];  //儲存到plist裡

    [selfviewDidLoad];

}

-(IBAction)edit:(id)sender 

{

    [dict setObject:@"hello"forKey:@"item2"];//修改

    [dict writeToFile:PATHatomically:YES];

    [selfviewDidLoad];    

}

- (IBAction)del:(id)sender 

{

    [dict removeObjectForKey:@"item5"];//刪除  

    [dict writeToFile:PATHatomically:YES];

    [selfviewDidLoad]; 

}

@end

 

代碼見 http://download.csdn.net/detail/x1135768777/4197663

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.