你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone開發:plist操作

iphone開發:plist操作

編輯:IOS開發綜合

想在一個類裡把個數組寫進.plist文件裡,再在另一個類裡從這個.plist文件把數組讀取出來?

以name,phoneNumber,age三個字段,為例。我是做的iphone,在文本框中輸入數據,獲取後,裝入數組中。然後把數組寫入.plist文件

寫操作

    NSString *name=[txt1 text];
    NSNumber *phoneNumber=[[NSNumber alloc] initWithInt:[[txt2 text] intValue]];
    NSNumber *age=[[NSNumber alloc] initWithInt:[[txt3 text] intValue]];
   
    NSMutableArray *array=[[NSMutableArray alloc]init];
    [array  addObject:name];
    [array  addObject:phoneNumber];
    [array  addObject:age];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];   
   
    [array writeToFile:filename  atomically:YES];
    [array release];


讀操作
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];
   
    NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
   
    txt1.text=[array objectAtIndex:0];
    txt2.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:1] intValue]];
    txt3.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:2] intValue]];

    [array release];

 

 

摘自 雲懷空-abel

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved