在之前的工作当中,曾经需要配置一个Plist文件,当时傻傻的一条一条输入,耗费了很长的时间.后来公司大神教了一个很好地方法,现在特此记录一下
NSArray *name = [[NSArray alloc]initWithObjects:@"Achafexp",@"Bonkers",@"carolingia",@"catholicschoolgirls",@"Chubsy",@"devroye",@"donreesclaws",@"feenacasual",@"Flakes",@"Gangster",@"gothic_ultra_ot",@"Howardson",@"Husser",@"JustOldFashion",@"Kells_SD",@"Kensinton",@"Kookazoo",@"littlelordfontleroy",@"Mothproof_Script",@"Nuttipy",@"OldSchool",@"oliver",@"Percirk",@"readyformycloseup",@"Reginald",@"SFCollegiateSolid",@"Tangerine_Bold",@"Zainly", nil];
NSLog(@"%d",name.count);
NSMutableArray *all = [[NSMutableArray alloc]initWithCapacity:0];
for (int i = 0; i<28; i++) {
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:[name objectAtIndex:i],@"font",@"hello",@"text", nil];
[all addObject:dict];
}
NSString *tempPath = NSTemporaryDirectory();
NSString *path = [tempPath stringByAppendingPathComponent:@"EnFontPList.plist"];
[all writeToFile:path atomically:YES];
//其核心,主要就是
NSString *path = [tempPath stringByAppendingPathComponent:@"EnFontPList.plist"];
[all writeToFile:path atomically:YES];
atomically参数解释
这个参数意思是如果为YES则保证文件的写入原子性,就是说会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.
如果为NO,则直接写入目标文件里.
这样就可以生成一个plist啦,很方便是不是,哈哈~
源码:Plist 动态写入DEMO