plist文件存储

1、根据plist文件生成tableView的静态单元格并保存开关状态的效果图

Untitled.gif

2、plist文件示意图

plist文件存储_第1张图片
Snip20161107_3.png
plist文件存储_第2张图片
Snip20161107_4.png

3、plist文件存储的实现步骤

3.1手动创建plist文件

plist文件存储_第3张图片
Snip20161107_5.png

*** 说明:plist文件(属性列表)是一种XML格式的文件,拓展名为plist;如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中***

3.2给plist文件添加内容作为tableView的数据源数组

plist文件存储_第4张图片
Snip20161107_9.png

3.3从plist文件中读取数据的核心代码

-(NSArray *)groups{
    if (_groups ==nil) {
        // 1.获取文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:self.plistName ofType:@"plist"];
        _groups = [NSArray arrayWithContentsOfFile:filePath];
    }
    return _groups;
}

选中单元格cell跳转到下一个控制器
// 选中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *group = self.groups[indexPath.section];
   
    NSArray *items = group[@"items"];
    // 获取每行cell的信息
    NSDictionary *item = items[indexPath.row];
    
    NSString *vcString = item[@"targetVC"];
    
    if (vcString && vcString.length >0 ) {
        // 根据字符串创建和字符串名字相同的类
        Class cla = NSClassFromString(vcString);
        UIViewController *targetVC = [[cla alloc]init];
        if ([targetVC isKindOfClass:[CLSettingViewController class]]) {
            CLSettingViewController *settingVC  = (CLSettingViewController * ) targetVC;
            settingVC.plistName = item[@"plistName"];
        }
        // 设置控制器的标题
        targetVC.title = item[@"title"];
        
        [self.navigationController pushViewController:targetVC animated:YES];
    }
}

3.4使用偏好设置NSUserDefaults保存开关的状态

// 重写setter方法
- (void)setItem:(NSDictionary *)item{

    _item = item ;
    // 设置cell的title
   self.textLabel.text = item[@"title"];
    
    //设置cell的图片
    self.imageView.image = [UIImage imageNamed:item[@"icon"]];
    
    NSString * imageView = item[@"accessoryView"];//@"UIImageView"
    
    //根据字符串转化类
    Class cla = NSClassFromString(imageView);// UIImageView 类
    
    //根据类创建类的对象
    id obj = [[cla alloc] init];//创建类的对象
    
    if([obj isKindOfClass:[UIImageView class]])
    {
        UIImageView * imageView = (UIImageView *)obj;
        
        imageView.image = [UIImage imageNamed:@"arrow_right"];
        
        //图片框和图片大小一样
        [imageView sizeToFit];
    }else if ([obj isKindOfClass:[UISwitch class] ]){
    
        UISwitch *mySwitch = (UISwitch *)obj;
        // 从偏好设置获取值
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
        mySwitch.on = [userDefaults boolForKey:self.item[@"switchKey"]];
        // 给开关添加事件
        [mySwitch addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    
    //添加辅助view
    self.accessoryView = obj;

}
// 开关的点击事件
-(void)valueChanged:(UISwitch *)sender{

    //存储到偏好设置
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    //设置值
    [userDefaults setBool:sender.isOn forKey:self.item[@"switchKey"]];
    
    [userDefaults synchronize];
}

3.5代码创建plist文件


#import "CLContact.h"

@implementation CLContact
// 编码 使用kvc进行编码
- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
}
// 解码 从字符串获取对象
- ( instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self == [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name" ];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
    }
    return self;
}
@end
//  ViewController.m
//  代码创建plist存储数据
//
//  Created by JackChen on 2016/11/7.
//  Copyright © 2016年 chenlin. All rights reserved.
//

#import "ViewController.h"
#import "CLContact.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test];
  // 沙盒document文件路径
  NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
   // document中存储的plist文件的全路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"contact.plist"];
    
    CLContact *contact = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

    NSLog(@"%@----%@",contact.name,contact.phone);
}
-(void)test{
    CLContact *contact = [[CLContact alloc] init];
    contact.name = @"liudehua";
    contact.phone = @"1234";
    NSString *docDir= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [docDir stringByAppendingPathComponent:@"contact.plist"];
    
    [NSKeyedArchiver archiveRootObject:contact  toFile:filePath];    
    NSLog(@"%@",filePath);
}
@end

3.6代码运行效果

Snip20161107_11.png

*** command + shift + G打开finder前往文件夹 输入文件路径 打开plist文件 ***

plist文件存储_第5张图片
Snip20161107_12.png

你可能感兴趣的:(plist文件存储)