iOS中plist的創(chuàng)建,數(shù)據(jù)寫入與讀取 !

功能創(chuàng)建一個(gè)test.plist文件,textInput作為輸入,displayLabel作為顯示,有一個(gè)按鈕來觸發(fā)保持程序triggerStorage;


-(void)triggerStorage

{

displayLabel.text = textInput.text;

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths    objectAtIndex:0];

NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];   //獲取路徑

NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];  //讀取數(shù)據(jù)

NSLog(@"dic2 is:%@",dic2);

//創(chuàng)建一個(gè)dic,寫到plist文件里

NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:displayLabel.text,@"IP",nil]; //寫入數(shù)據(jù)

[dic writeToFile:filename atomically:YES];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

NSMutableArray *resultData;

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths objectAtIndex:0];

NSLog(@"path = %@",path);

NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];

//讀文件

NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];

NSLog(@"dic is:%@",dic2);

if(dic2 == nil)

{

//1. 創(chuàng)建一個(gè)plist文件

NSFileManager* fm = [NSFileManager defaultManager];

[fm createFileAtPath:filename contents:nil attributes:nil];

}

else

{

resultData=[dic2 objectForKey:@"IP"];

if([dic2 count] > 0)

{

displayLabel.text = resultData;

}

else

{

displayLabel.text = @" ";

}

}

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]   //按鈕的初始化及觸發(fā)條件設(shè)置

initWithTitle:@"保存"

style:UIBarButtonItemStylePlain

target:self

action:@selector(triggerStorage)] autorelease];

[super viewDidLoad];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,624評論 30 472
  • 《裕語言》速成開發(fā)手冊3.0 官方用戶交流:iApp開發(fā)交流(1) 239547050iApp開發(fā)交流(2) 10...
    葉染柒丶閱讀 28,763評論 5 20
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,255評論 2 33
  • 在這個(gè)提筆忘字的時(shí)代,在這泯滅一切的似水流年里,是否還能記起書寫的感覺,手指劃過紙張的觸感,心中流淌情感,思緒縈...
    不沸騰孩子閱讀 75評論 0 0

友情鏈接更多精彩內(nèi)容