标签:
plist :一般存储一般对象:NSString,字典,数组,NSData
第一,首先要设置2个按钮,基本知识,就不在这写了,直接写按钮的触发事件来存储和读取
- (IBAction)save
{
// 1.获得沙盒根路径
NSString *home = NSHomeDirectory();
// 2.document路径
NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
// 3.新建数据
NSArray *data = @[@"jack", @10, @"ffdsf"];
NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];
[data writeToFile:filepath atomically:YES];
}
//读取按钮
- (IBAction)read {
// 1.获得沙盒根路径
NSString *home = NSHomeDirectory();
// 2.document路径
NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
// 3.文件路径
NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];
// 4.读取数据
NSArray *data = [NSArray arrayWithContentsOfFile:filepath];
NSLog(@"%@", data);
}
NSUserDefault存储
- (IBAction)save {
// 1.利用NSUserDefaults,就能直接访问软件的偏好设置(Library/Preferences)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 2.存储数据
[defaults setObject:@"mj" forKey:@"account"];
[defaults setObject:@"123" forKey:@"pwd"];
[defaults setInteger:10 forKey:@"age"];
[defaults setBool:YES forKey:@"auto_login"];
// 3.立刻同步
[defaults synchronize];
}
- (IBAction)read {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *account = [defaults objectForKey:@"account"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];
NSLog(@"%@ -- %d", account, autoLogin);
}
NSKeyedArchiver存model方法
一般存储模型对象 model类的对象,不能存储一般对象(字典,字符串,数组等)
新建2个模型
模型里面有对应的数据
MJStudent
#import <Foundation/Foundation.h>
@interface MJStudent : NSObject <NSCoding> //遵守NSCoding协议
@property (nonatomic, copy) NSString *no;
@property (nonatomic, assign) double height;
@property (nonatomic, assign) int age;
在.m文件
@interface MJStudent()
@end
@implementation MJStudent
/**重要
* 将某个对象写入文件时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
//如果不写encoding方法,系统直接崩溃
//这个是保存数据的encoding方法
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.no forKey:@"no"];
[encoder encodeInt:self.age forKey:@"age"];
[encoder encodeDouble:self.height forKey:@"height"];
}
/**重要
* 从文件中解析对象时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
//这个是解析数据的方法
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
// 读取文件的内容
self.no = [decoder decodeObjectForKey:@"no"];
self.age = [decoder decodeIntForKey:@"age"];
self.height = [decoder decodeDoubleForKey:@"height"];
}
return self;
}
在viewcontroller里面
//保存数据按钮
- (IBAction)save {
// 1.新的模型对象
MJStudent *stu = [[MJStudent alloc] init];
stu.no = @"42343254";
stu.age = 20;
stu.height = 1.55;
// 2.归档模型对象
// 2.1.获得Documents的全路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.2.获得文件的全路径
NSString *path = [doc stringByAppendingPathComponent:@"stu.data"];
// 2.3.将对象归档
[NSKeyedArchiver archiveRootObject:stu toFile:path];
}
//读取数据按钮
- (IBAction)read {
// 1.获得Documents的全路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.获得文件的全路径
NSString *path = [doc stringByAppendingPathComponent:@"stu.data"];
// 3.从文件中读取MJStudent对象
MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@ %d %f", stu.no, stu.age, stu.height);
}
三种数据存储 (plist,NSUserDefaults存储,NSKeyedArchiver存模型方法)
标签:
原文地址:http://www.cnblogs.com/liaolijun/p/4565292.html