很多时候把数据存到plist文件里,用的时候去取不失为一种简洁方便的好办法,那下面我们就学习下如何快速生成plist文件吧。
-
首先建一个继承NSObject的对象
- 其次,在writePlistFile.m中写以下代码:
+(void)writePlistFilesOfresourceName:(NSString*)resorceName resourceType:(NSString*)type PlistName:(NSString*)plistName{
//获取json 文件
NSString *txtPath = [[NSBundle mainBundle] pathForResource:resorceName ofType:type];
NSData *jsonData = [NSData dataWithContentsOfFile:txtPath options:NSDataReadingMappedIfSafe error:nil];
NSMutableDictionary *nameDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSString *resultPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]];
NSLog(@"resultPath:\n%@",resultPath);
//写入文件
[nameDict writeToFile:resultPath atomically:YES]; }
- 最后,在Appdelegate里调用此方法,根据打印出的路径去找到plist文件,拉到所需的项目中,就是这么easy!!!