码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习笔记13-字典数据写入plist

时间:2016-02-22 16:02:22      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

以上为字典数据(图片)

 

首先求到应用程序包的路径

 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(@"写入成功....");

    }

    

 

iOS学习笔记13-字典数据写入plist

标签:

原文地址:http://www.cnblogs.com/adodo/p/5207157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!