1.Preference 存储方式(偏好设置)
//Preference 存储方式
-(void)save1{
//根据系统的方式创建
//NSUserDefaults *userdf = [[NSUserDefaults standardUserDefaults];
//自定义文件的名字
NSUserDefaults *userdf = [[NSUserDefaults alloc]initWithSuiteName:@"myname"];
[userdf setValue:@"LK" forKey:@"name"];
[userdf setBool:1 forKey:@"sex"];
[userdf setInteger:190 forKey:@"age"];
[userdf setObject:@[@"value1",@"value2",@"value3"] forKey:@"arr"]; //可以保存数组
[userdf synchronize]; //同步保存
}
-(void)read1{
//NSUserDefaults *userdf = [[NSUserDefaults standardUserDefaults];
NSUserDefaults *userdf = [[NSUserDefaults alloc]initWithSuiteName:@"myname"];
NSString *name = [userdf stringForKey:@"name"];
BOOL sex = [userdf boolForKey:@"sex"];
NSInteger age = [userdf integerForKey:@"age"];
NSArray *arr = [userdf arrayForKey:@"arr"];
NSLog(@"name:%@;sex:%zd;age:%ld;arr:%@",name,sex,age,arr);
}
2.plist归档存储
//plist归档存储
-(void)save2{
//第一个参数 分别对应沙盒目录下的位置 NSCachesDirectory、NSDocumentDirectory、、、、
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *data = @[@"name",@"age",@"sex"];
[data writeToFile:[[docPath lastObject] stringByAppendingPathComponent:@"lk.plist"] atomically:YES];
}
-(void)read2{
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *data = [NSArray arrayWithContentsOfFile:[[docPath lastObject] stringByAppendingPathComponent:@"lk.plist"]];
NSLog(@"%@",data);
}
3.对象存储
Book.h 对象代码:
#import <Foundation/Foundation.h>
@interface Book : NSObject<NSCoding>
+(instancetype)initWithName:(NSString *)name Price:(double)price Number:(int)number;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) double price;
@property (nonatomic,assign) int number;
@end
Book.m 对象代码:
#import "Book.h"
@implementation Book
+(instancetype)initWithName:(NSString *)name Price:(double)price Number:(int)number{
Book *book = [[Book alloc]init];
book.name = name;
book.price = price;
book.number = number;
return book;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.number forKey:@"number"];
[aCoder encodeDouble:self.price forKey:@"price"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.number = [aDecoder decodeIntForKey:@"number"];
self.price = [aDecoder decodeDoubleForKey:@"price"];
}
return self;
}
-(NSString *)description{
return [NSString stringWithFormat:@"name:%@;number:%d,%0.2f",_name,_number,_price];
}
@end
//保存对象 NSKeyedArchiver归档 对象需要实现(NSCoding)
-(void)save3{
// Book *book = [[Book alloc]init];
// book.name = @"good";
// book.price = 19.8;
// book.number = 100;
Book *book1 = [Book initWithName:@"book1" Price:29.89 Number:200];
Book *book2 = [Book initWithName:@"book2" Price:19.89 Number:23];
NSArray *data = @[book1,book2];
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[NSKeyedArchiver archiveRootObject:data toFile:[doc stringByAppendingPathComponent:@"book"]];
}
-(void)read3{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSArray *data = [NSKeyedUnarchiver unarchiveObjectWithFile:[doc stringByAppendingPathComponent:@"book"]];
NSLog(@"%@",data);
}
4.保存多个对象
//保存多个对象
-(void)save4{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Book *book1 = [Book initWithName:@"book1" Price:29.89 Number:200];
Book *book2 = [Book initWithName:@"book2" Price:19.89 Number:23];
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:book1 forKey:@"book1"];
[archiver encodeObject:book2 forKey:@"book2"];
[archiver finishEncoding];
[data writeToFile:[doc stringByAppendingPathComponent:@"ndata"] atomically:YES];
}
-(void)read4{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSData *data = [NSData dataWithContentsOfFile:[doc stringByAppendingPathComponent:@"ndata"]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
Book *b1 = [unarchiver decodeObjectForKey:@"book1"];
Book *b2 = [unarchiver decodeObjectForKey:@"book2"];
NSLog(@"%@",b1);
NSLog(@"%@",b2);
[unarchiver finishDecoding];
}