iOS开发中怎么创建Plist文件
2017-09-26
展开全部
iOS中plist的创建,数据写入与读取功能创建一个test.plist文件,textInput作为输入,displayLabel作为显示,有一个按钮来触发保持程序triggerStorage;-(void)triggerStorage{displayLabel.text=textInput.text;NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*path=[pathsobjectAtIndex:0];NSString*filename=[pathstringByAppendingPathComponent:@"test.plist"];//获取路径NSDictionary*dic2=[NSDictionarydictionaryWithContentsOfFile:filename];//读取数据NSLog(@"dic2is:%@",dic2);//创建一个dic,写到plist文件里NSDictionary*dic=[NSDictionarydictionaryWithObjectsAndKeys:displayLabel.text,@"IP",nil];//写入数据[dicwriteToFile:filenameatomically:YES];}//ImplementviewDidLoadtodoadditionalsetupafterloadingtheview,typicallyfromanib.-(void)viewDidLoad{NSMutableArray*resultData;NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*path=[pathsobjectAtIndex:0];NSLog(@"path=%@",path);NSString*filename=[pathstringByAppendingPathComponent:@"test.plist"];//读文件NSDictionary*dic2=[NSDictionarydictionaryWithContentsOfFile:filename];NSLog(@"dicis:%@",dic2);if(dic2==nil){//1.创建一个plist文件NSFileManager*fm=[NSFileManagerdefaultManager];[fmcreateFileAtPath:filenamecontents:nilattributes:nil];}else{resultData=[dic2objectForKey:@"IP"];if([dic2count]>0){displayLabel.text=resultData;}else{displayLabel.text=@"";}}self.navigationItem.rightBarButtonItem=[[[UIBarButtonItemalloc]//按钮的初始化及触发条件设置initWithTitle:@"保存"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(triggerStorage)]autorelease];[superviewDidLoad];}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询