从plist文件中读取数据

//从plist文件中读取数据
- (void)readDataFromPlist
{
    //1.先获取文件路径
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Book" ofType:@"plist"];
    //2.根据路径初始化字典对象
    self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    
    //将不可边字典变成可变字典
    
   self.addressBook = [NSMutableDictionary dictionaryWithDictionary:_dic];
    
    //将字典封装成对象
    //(1)从字典中遍历出key
    for (NSString * key in self.dic) {
        //根据key获取对应的数组
        NSArray * groupArr =self.dic[key];
        
        //(5) 创建可变的小数组,存储封装的对象
        NSMutableArray * newGroupArr = [NSMutableArray array];
        //(3)获取小数组中元素(遍历小数组)
        for (NSDictionary * groupDic in groupArr) {
            //(4)通过字典初始化AddressPerson
            AddressPerson * Person = [[AddressPerson alloc]initWithDic:groupDic];
                        
            [newGroupArr addObject:Person];//将对象存储到新的可变数组中
            RELEASE_SAFE(Person);
        }
        //将要原来key对应的存储字典的小数组替换成装有对象的心得可变的数组
        
        [self.addressBook setObject:newGroupArr forKey:key];
    }
    
    //取出字典中所有的key
    self.orderedKeys= [[self.addressBook allKeys]sortedArrayUsingSelector:@selector(compare:)];
   // NSLog(@"%@",addressBook);
       
}


你可能感兴趣的:(文件,读取数据,plist)