轻量级数据持久化的几种方式

1、plist文件

/**

描述:plist文件持久化,存一些小型的文件。

plist文件是将某些特定的类,通过**XML**文件的方式保存在目录中。

NSArray;//数组

NSMutableArray;//可变数组

NSDictionary;//字典

NSMutableDictionary;//可变字典

NSData;//二进制数据

NSMutableData;//可变二进制数据

NSString;//字符串

NSMutableString;//可变字符串

NSNumber;//基本数据

NSDate;//日期

*/

// 存储

[self writeToPlist];

// 取出

NSArray *result = [self readFromPlist];

NSLog(@"取出来的plist文件%@",result);

/**

读写文件-plist

*/

- (void)writeToPlist{

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;

NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];

NSArray *array = @[@"1234", @"tony", @"java"];

//序列化,把数组存入plist文件

[array writeToFile:fileName atomically:YES];

}

- (NSArray *)readFromPlist{

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;

NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];

//反序列化,把plist文件数据读取出来,转为数组

NSArray *result = [NSArray arrayWithContentsOfFile:fileName];

return result;

}

2、NSUserDefaults

/**

描述:NSUserDefaults

如果没有调用synchronize方法,系统会根据I/O情况不定时刻地保存到文件中。所以如果需要立即写入文件的就必须调用synchronize方法。

偏好设置会将所有数据保存到同一个文件中,使用同一个key进行存储数据,会覆盖之前存储的数据。

存的都是不可变的。

*/

//1.获得NSUserDefaults文件

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

//2.向偏好设置中写入内容

[userDefaults setObject:@"AAA" forKey:@"a"];

[userDefaults setBool:YES forKey:@"sex"];

[userDefaults setInteger:21 forKey:@"age"];

//2.1立即同步

[userDefaults synchronize];

//3.读取偏好设置

NSString *name = [userDefaults objectForKey:@"a"];

BOOL sex = [userDefaults boolForKey:@"sex"];

NSInteger age = [userDefaults integerForKey:@"age"];

NSLog(@"NSUserDefaults读取数据:%@, %d, %ld", name, sex, age);

3、归档与反归档

创建model对象:

Person.h

#import// `encodeWithCoder`:用来说明如何将对象编码到归档中。 `initWithCoder`:用来说明如何进行解档来获取一个新对象。@interface Person : NSObject//2.设置属性

@property (strong, nonatomic) NSString *name;

@property (strong, nonatomic) NSString *passWord;

+ (Person *)userWithName:(NSString *)name password:(NSString *)password;

Person.m

+ (Person *)userWithName:(NSString *)name password:(NSString *)password

{

Person *user = [[Person alloc]init];

user.name = name;

user.passWord = password;

return user;

}

//归档

- (void)encodeWithCoder:(NSCoder *)aCoder {

[aCoder encodeObject:self.name forKey:@"name"];

[aCoder encodeObject:self.passWord forKey:@"passWord"];

}

//解档

- (id)initWithCoder:(NSCoder *)aDecoder{

if (self = [super init]) {

self.name = [aDecoder decodeObjectForKey:@"name"];

self.passWord = [aDecoder decodeObjectForKey:@"passWord"];

}

return self;

}

/**

描述:归档反归档

*/

// 归档

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;

NSString *file = [path stringByAppendingPathComponent:@"person.data"];

Person *person = [Person userWithName:@"xiaowang" password:@"110"];

Person *person1 = [Person userWithName:@"xiaozhang" password:@"119"];

NSMutableArray *personsArr = [NSMutableArray arrayWithObjects:person, person1, nil];

// model数组存储

[NSKeyedArchiver archiveRootObject:personsArr toFile:file];

// 解档

NSString *pathResult = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;

NSString *fileResult = [pathResult stringByAppendingPathComponent:@"person.data"];

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:fileResult];

for (Person *per in array) {

if (per) {

NSLog(@"归档反归档:%@\n%@",per.name,per.passWord);

}

}

你可能感兴趣的:(轻量级数据持久化的几种方式)