iOS中的几种数据存储方式(plist存储、偏好设置存储、归档存储)

1.plist存储

plist存储可以存储系统自带的对象比如NSAaary、NSDictionary等,一般可以写出writeToFile:这个方法的对象都可以使用plist存储.一般写入到Document文件夹.
plist存储的写入:

    <span style="font-size:18px;">NSArray *arr = @[@"Chinese",@"Japan",@"Ausrtalia"];
    //获取Document文件夹路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *completePath = [documentPath stringByAppendingPathComponent:@"names"];
    [arr writeToFile:completePath atomically:YES];</span>

plist存储的读取:
<strong>    </strong>NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *completePath = [documentPath stringByAppendingPathComponent:@"names"];
    NSArray *resultArr = [NSArray arrayWithContentsOfFile:completePath];
    NSLog(@"%@",resultArr);

2.偏好设置存储(NSUserDefaults)

偏好设置一般存储一些像账号密码之类的东西,写入到Preference文件夹.
偏好设置存储的写入:(写入的时候可以根据写入数据的类型来选择对应的方法)
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@"China" forKey:@"china"];
    [userDefaults setInteger:1 forKey:@"1"];
    [userDefaults setBool:YES forKey:@"yes"];
    //立即存储
    [userDefaults synchronize];
偏好设置存储的读取:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *china = [defaults valueForKey:@"china"];
    NSInteger number = [defaults integerForKey:@"1"];
    BOOL bu = [defaults boolForKey:@"yes"];
    NSLog(@"%@--%d,%ld",china,bu,number);

3.归档存储

归档存储一般存储自定义对象,并且自定义对象需要遵守NSCoding协议.
首先创建一个自定义对象MCPerson,并且让其遵守NSCoding协议.
#import <Foundation/Foundation.h>

@interface MCPerson : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *phone;
@end
在.m文件中实现NSCoding协议的两个方法.
#import "MCPerson.h"

@implementation MCPerson
//编码
- (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
注意事项:如果父类也遵守了NSCoding协议,那么需要在encodeWithCoder:方法中加上[super encodeWithCoder:aCoder].在initWithCoder:方法中将self = [super init]写成self = [super initWithCoder:aDecoder].确保继承自父类的实例变量也能被解码和解码.
归档存储的写入:
    MCPerson *person = [[MCPerson alloc]init];
    person.name = @"jack";
    person.phone = @"12425626236";
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *completePath = [documentPath stringByAppendingPathComponent:@"person"];
    [NSKeyedArchiver archiveRootObject:person toFile:completePath];
归档存储的读取:
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *completePath = [documentPath stringByAppendingPathComponent:@"person"];
    MCPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:completePath];
    NSLog(@"%@--%@",person.name,person.phone);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值