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

时间:2022-09-11 23:16:22

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);