iOS学习笔记13-字典数据写入plist
以上为字典数据(图片)
首先求到应用程序包的路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
然后将路径下所有的文件读出
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *allFile = [fm contentsOfDirectoryAtPath:path
error:nil];
NSLog(@"%@",allFile);
找出有图片的文件,并加入数组
NSMutableArray *imgFiles = [[NSMutableArray alloc]init];
[allFile enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// NSLog(@"%@",obj);
NSString *file = (NSString *)obj;
if ([[file pathExtension] isEqualToString:@"jpg"]) {
NSString *appName = [file stringByDeletingPathExtension];//去掉扩展名
//将数据封装成为字典
NSDictionary *dic = @{@"appName":appName,@"appIcon":file};
//字典放到数组中
[imgFiles addObject:dic];
}
}];
得到文件的路径,并写入
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *document = arr[0];
NSLog(@"%@",document);
NSString *plistPath = [document stringByAppendingPathComponent:@"/imgStore.plist"];
NSFileManager *imgFileManager = [NSFileManager defaultManager];
[imgFileManager createFileAtPath:plistPath
contents:nil
attributes:nil];
BOOL r = [imgFiles writeToFile:plistPath atomically:YES];
if (r) {
NSLog(@"写入成功....");
}