iOS开发之数据存储

一、iOS应用数据存储的常用方式

- XML属性列表(plist)归档
- Preference(偏好设置) 本质还是通过“plist”来存储数据, 但是使用更简单(无需关注文件、文件夹路径和名称)
- NSKeyedArchiver归档(NSCoding) 把任何对象, 直接保存为文件的方式。
- SQLite3 当非常大量的数据存储时使用
- Core Data 就是对SQLite的封装   

二、获取路径

    - 获取bundle路径

      NSString *path = [NSBundle mainBundle].bundlePath;

    -获取沙盒路径

      NSString *home = NSHomeDirectory();

三、沙盒路径目录结构

    -Document          保存应用运行时生成的需要持久化的数据
    -library
        Caches         保存应用运行时生成的需要持久化的数据
        Preferences    保存应用的所有偏好设置
    -temp              保存应用运行时所需的临时数据

四、利用沙盒根目录拼接”Documents”字符串

  • 方式一:(拼接字符串)

    NSString *home =NSHomeDirectory();
    NSString *documents = [home stringByAppendingString:@“/Documents"];
    
  • 方式二:(作为路径的一部分)

    NSString *home =NSHomeDirectory();
    NSString*documents =[homestringByAppendingPathComponent:@"Documents"];
    
  • 方式三:()

    参数1:目标文件,参数二:作用域, 参数三:是否展开波浪线
    
    [NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)]
    
    NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    

五、获取caches文件夹路径

//获取caches文件夹

NSString * cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, NO) lastObject];

六、获取偏好设置文件夹路径

  NSTemporaryDirectory()

七、plist数据的存储与读取

  • //数据存储

        NSArray * names = [NSArray arrayWithObjects:@"Bob",@"王宝", @"球童",@"Jack",nil];
    
      // 1.获取路径
     NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
      // 2.拼接文件名
    
    NSString * filePath = [path stringByAppendingPathComponent:@"names.plist"];
    
    // 3. 存储
    // atomically 是否允许原子写入
    [names writeToFile:filePath atomically:YES];
    
    NSLog(@"%@",filePath);
    

  • 读取数据

    //1.获取数据路径
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //2.拼接文件路径
    
    NSString * filePath = [path stringByAppendingPathComponent:@"names.plist"];
    
    //3.读取数据
    
    NSArray * names = [NSArray arrayWithContentsOfFile:filePath];
    
    NSLog(@"%@",names);
    

八、plist数据的存储与读取

  • //偏好设置存储

    //1.获取NSUserDefaults对象
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    //2.存储数据
    [userDefaults setObject:@"Jack" forKey:@"name"];
    
    [userDefaults setInteger:23 forKey:@"age"];
    
    [userDefaults setBool:YES forKey:@"sex"];
    
    //3.立即存储
    [userDefaults synchronize];
    
    NSLog(@"%@",NSHomeDirectory());
    

  • //读取偏好设置

    //1.获取NSUserDefaults对象
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    //2.读取数据
    NSString * name = [userDefaults objectForKey:@"name"];
    
    NSInteger age = [userDefaults integerForKey:@"age"];
    
    BOOL sex = [userDefaults boolForKey:@"sex"];
    
    NSLog(@"name =%@  age = %ld sex = %d",name,age,sex);
    

九、归档与反归档

“归档”:是一种可以把任何对象,直接保存为文件的方式。

- 如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复
- 不是所有的对象都可以直接用这种方法进行归档,只有遵守了NSCoding协议的对象才可以
- NSCoding协议有2个方法:
- encodeWithCoder:

     每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey:方法归档实例变量

- initWithCoder:

 每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量

CZPerson.h
[x] 注意:改方法要遵守NSCodiing协议

#import <Foundation/Foundation.h>

@interface CZPerson : NSObject<NSCoding
@property (nonatomic,copy) NSString * name;
@property (nonatomic,copy) NSString * phone;
@end

CZPerson.m
[x] 注意:实现NSCodiing协议的方法*

#import "CZPerson.h"

@implementation CZPerson
//确定要存储对象的哪些属性
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
}
//确定要读取对象的哪些属性

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
    }
    return self;
}
@end

ViewController.m

#import "ViewController.h"
#import "CZPerson.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //反归档 (反序列化)

    //1.获取路径
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //2.拼接文件名

    NSString * filePath = [path stringByAppendingPathComponent:@"person"];

    //3.通过反归档读取数据

    CZPerson * p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"name =%@  phone = %@",p.name,p.phone);
//    [self test01];
}
//归档 (序列化)
- (void) test01
{
    //创建person对象
    CZPerson * person = [[CZPerson alloc] init];

    //赋值
    person.name = @"JackMeng";
    person.phone = @"186000001";

    //获取路径
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //拼接文件名
    NSString * filePath = [path stringByAppendingPathComponent:@"person"];

    // 归档

    [NSKeyedArchiver archiveRootObject:person toFile:filePath];

    NSLog(@"%@",NSHomeDirectory());
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ViewController.h

#import "ViewController.h"
#import "CZPerson.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //反归档 (反序列化)

    //1.获取路径
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //2.拼接文件名

    NSString * filePath = [path stringByAppendingPathComponent:@"person"];

    //3.通过反归档读取数据

    CZPerson * p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"name =%@  phone = %@",p.name,p.phone);
//    [self test01];
}
//归档 (序列化)
- (void) test01
{
    //创建person对象
    CZPerson * person = [[CZPerson alloc] init];

    //赋值
    person.name = @"JackMeng";
    person.phone = @"186000001";



    //获取路径
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //拼接文件名
    NSString * filePath = [path stringByAppendingPathComponent:@"person"];

    // 归档

   [NSKeyedArchiver archiveRootObject:person toFile:filePath];

    NSLog(@"%@",NSHomeDirectory());
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值