一、Plist方式数据存取:
1.保存NSArray到document/plist中:
/*
保存NSArray到document/plist中
*/
- (void)saveNSArrayToPlist{
//获取沙盒根路径
NSString *home = NSHomeDirectory();
//document路径
NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
//新建数据
NSArray *data = @[@"姓名", @26, @"工程师"];
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
[data writeToFile:filePath atomically:YES];
}
2.从document/plist中读取NSArray数据:
/*
从document/plist中读取NSArray数据
*/
- (void)readPlistConvertNSArray{
//获取沙盒根路径
NSString *home = NSHomeDirectory();
//document路径
NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
//文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
//读取数据
NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
}
二、Preferences方式数据存取:
1.保存数据到Preferences(类试SharedPreference):
/*
保存数据到Preferences(类试SharedPreference)
*/
- (void)savePreferences{
//获取NSUserDefaults对象
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
//存储数据,key=value形式
[pref setObject:@"值内容" forKey:@"key名称"];
[pref setInteger:1 forKey:@"key名称"];
[pref setBool:YES forKey:@"key名称"];
}
2.读取Preferences数据(类试SharedPreference):
/*
读取Preferences数据(类试SharedPreference)
*/
- (void)readPreferences{
//获取NSUserDefaults对象
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString *value = [pref objectForKey:@"key名称"];
BOOL isValue = [pref boolForKey:@"key名称"];
}
三、NSKeyedArchiver方式,单个对象到单个文件数据存取:
1.User.h文件中定义几个变量,实现NSCoding协议,代码:
#import <Foundation/Foundation.h>
@interface User : NSObject <NSCoding>
@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSInteger age;
@property(nonatomic, assign) BOOL isLogin;
@end
2.重写encodeWithCoder方法,保存各个属性值,重写initWithCoder方法,取出缓存中的值赋值给属性:
#import "User.h"
@implementation User
/*
保存对象到缓存中时会调此方法
*/
- (void)encodeWithCoder:(NSCoder *)encoder{
/*
保存各个值
*/
[encoder encodeObject:_name forKey:@"name"];
[encoder encodeInteger:_age forKey:@"age"];
[encoder encodeBool:_isLogin forKey:@"isLogin"];
}
/*
读取缓存中的此对象时会调此方法
*/
- (id)initWithCoder:(NSCoder *)decoder{
if(self = [super init]){
/*
获取各个存储的值,并赋值
*/
_name = [decoder decodeObjectForKey:@"name"];
_isLogin = [decoder decodeBoolForKey:@"isLogin"];
_age = [decoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
3.调用保存与读取方法:
/*
保存对象
*/
- (void)saveObj{
User *user = [[User alloc] init];
user.name = @"姓名";
user.age = 26;
user.isLogin = YES;
//获得Documents绝对路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//获取路径
NSString *path = [doc stringByAppendingPathComponent:@"user.data"];
//保存对象
[NSKeyedArchiver archiveRootObject:user toFile:path];
}
/*
读取对象
*/
- (void)readObj{
//获得Documents绝对路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//获取路径
NSString *path = [doc stringByAppendingPathComponent:@"user.data"];
//读取对象
User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
四、NSKeyedArchiver方式,多个对象到同个文件的存取:
1.将多个对象存到同一个文件:
/*
将多个对象存到同一个文件
*/
- (void)saveMultipleObj{
//新建可变NSData
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *ka = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
User *user1 = nil;
User *user2 = nil;
//将多个对象存到NSMutableData
[ka encodeObject:user1 forKey:@"key名称1"];
[ka encodeObject:user2 forKey:@"key名称2"];
//完成存储
[ka finishEncoding];
//获得Documents绝对路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//获取路径
NSString *path = [doc stringByAppendingPathComponent:@"user.data"];
//将NSMutableData中的数据存到文件
[data writeToFile:path atomically:YES];
}
2.读取文件中的多个对象:
/*
读到文件中的多个对象
*/
- (void)readMultipleObj{
//获得Documents绝对路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//获取路径
NSString *path = [doc stringByAppendingPathComponent:@"user.data"];
//根据路径创建NSData
NSData *data = [NSData dataWithContentsOfFile:path];
//创建NSKeyedUnarchiver
NSKeyedUnarchiver *ku = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//根据key获取对象
User *user1 = [ku decodeObjectForKey:@"key名称1"];
User *user2 = [ku decodeObjectForKey:@"key名称2"];
//读取完成
[ku finishDecoding];
}