iOS開發中plist檔案的建立與簡單讀取(一)

來源:互聯網
上載者:User

iOS開發中plist檔案的建立與簡單讀取(一)

 

 

1. 建立一個工程

2. 建立一個plist檔案(command+N快速鍵或者建立檔案 下的紅色圈)

 

3. 紅色圈是plist的檔案名稱,這裡預設為Property List,點擊建立就可以了

4 。plist檔案如下所示

5 類型為字典類型

6 兩個字典建立好了,注意這裡給字典裡添加資料時,要點擊teacher的三角符號朝下時,點擊“+”,這樣才建立在字典裡,不按照這樣做,會在字典外面。

7 添加資料

8 plistde的檔案建立好了,建立一個新的檔案

studentViewController

 

studentViewController.m

 

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.        NSString* plistfile1 = [[NSBundle mainBundle]pathForResource:@Property List ofType:@plist];    NSMutableDictionary* data = [[NSMutableDictionary alloc]initWithContentsOfFile:plistfile1];    //列印出字典裡的資料    NSLog(@%@,data);    [data setObject:@add some content forKey:@c_key];        //擷取應用沙箱的Douch    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);    NSString* plist1 = [paths objectAtIndex:0];        //擷取一個plist檔案    NSString* filename = [plist1 stringByAppendingString:@test.plist];    [data writeToFile:filename atomically:YES];    NSMutableDictionary* data1 = [[NSMutableDictionary alloc]initWithContentsOfFile:filename];    //列印出字典裡的資料    NSLog(@%@,data1);        //修改一個plist檔案的資料    [data1 setObject:@要修改的數值 forKey:@要修改的name];    [data1 writeToFile:filename atomically:YES];        }

輸出結果

 

 

2014-04-15 18:50:17.468 plistfile[1531:a0b] {    student =     {        Sname = lucy;        Snum = 1;    };    teacher =     {        Tname = jhon;        Tnum = 2;    };}2014-04-15 18:50:17.484 plistfile[1531:a0b] {    c_key = add some content;    student =     {        Sname = lucy;        Snum = 1;    };    teacher =     {        Tname = jhon;        Tnum = 2;    };}


 


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.