Iphone文件处理——使用属性列表(plist)

先在Resources文件夹下新建一个Movies.plist文件,然后往里面添加一些内容。

然后在viewDidLoad方法里面添加如下内容,这样就可以做到如果Documents文件夹下有Movies文件那么就将其内容打印出来,如果没有就修改Resources文件下的Movies文件,然后将其复制到Documents文件夹下:

- (void)viewDidLoad
    //下面这两句获取Documents文件夹路径
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [path objectAtIndex:0];
    //添加一个Movies.plist文件在Documents文件夹下
    NSString *plistFileName = [documentsDir stringByAppendingPathComponent:@"Movies.plist"];
    if ([[NSFileManager defaultManager]fileExistsAtPath:plistFileName]) {//如果这个文件存在,那么就将文件内容打印出来
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plistFileName];
        for (NSString *category in dict) {
            NSLog(@"%@",category);
            NSArray *titles = [dict valueForKey:category];
            for(NSString *title in titles){
                NSLog(@"%@",title);
            }
        }
        [dict release];
    }
    else{//如果文件不存在,就先修改Resources文件夹下的Movies.plist文件,在每一个array下添加以项,然后复制到Documents文件夹下
        NSString *plisPath  = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plisPath];
        NSMutableDictionary *copyofdict = [dict mutableCopy];
        NSArray *categoriesArray = [[copyofdict allKeys]sortedArrayUsingSelector:@selector(compare:)];
        
        for(NSString *category in categoriesArray){
            NSArray *title = [dict valueForKey:category];
            NSMutableArray *mutabletitles = [title mutableCopy];
            [mutabletitles addObject:@"helijun"];
            [copyofdict setObject:mutabletitles forKey:category];
            [mutabletitles release];
        }
        NSString *fileName = [documentsDir stringByAppendingPathComponent:@"Movies.plist"];
        [copyofdict writeToFile:fileName atomically:YES];
        [dict release];
        [copyofdict release];
    }
  [super viewDidLoad];
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值