沙盒存儲——plist
1111.png
屬性列表是一種XML格式的文件志秃,拓展名為plist房待。如果對象是NSString邢羔、NSDictionary驼抹、NSArray、NSData拜鹤、NSNumber等類型框冀,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中
如果是其他類型的的對象,需要使用plist儲存敏簿,需要轉(zhuǎn)類型:
例如://轉(zhuǎn)化Data
NSData * imageData = UIImagePNGRepresentation(getImage);
存:
//獲取路徑
// NSSearchPath尋找路徑
// ForDirectories要找的文件夾
// InDomains在哪個地方找
// NSDocumentDirectory Documents
NSString *docPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
//拼接路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];
//創(chuàng)建字典
NSDictionary *dict =@{@"name":@"laoma",@"age":@"18"};
// 存儲字典
[dict writeToFile:filePath atomically:YES];
让饕病:
//獲取doc路徑
NSString *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
//拼接plist路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"xxxx.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@",dict[@"name"]);
字符串存取
- (IBAction)save:(id)sender {
//獲取docpath
NSString *docpath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
//拼接路徑
NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];
NSString *str =@“hello word"
;
[str writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
- (IBAction)read:(id)sender {
//獲取docpath
NSString *docpath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
//拼接路徑
NSString *filepath = [docpath stringByAppendingPathComponent:@"x.plist"];
// 獲取字符串
NSString *str = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str);
}
沙盒儲存——?dú)w檔解檔
#import<Foundation/Foundation.h>
@interface Person :NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)NSInteger age;
@end
#import"Person.h"
@implementationPerson
//告訴系統(tǒng)歸檔哪些屬性
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
}
//告訴系統(tǒng)解檔哪些屬性,如何解檔
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if(self= [super init]) {
_name= [aDecoder decodeObjectForKey:@"name"];
_age= [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
#import "ViewController.h"
#import "Person.h"
@interface ViewController()
@end
@implementation ViewController
//歸檔
- (IBAction)save:(id)sender {
//創(chuàng)建對象
Personn*p = [[Person alloc]init];
p.name=@"zhangsan";
p.age=18;
// docpath
NSString *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
// 拼接路徑
NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];
//歸檔
[NSKeyedArchiver archiveRootObject:p toFile:fliePath];
}
//解檔
- (IBAction)read:(id)sender {
// docpath
NSString *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
// 拼接路徑
NSString *fliePath = [docPath stringByAppendingPathComponent:@"person.data"];
//解檔
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fliePath];
NSLog(@"%@",p.name);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.}
@end
沙盒存儲——偏好設(shè)置userDefaults
保存設(shè)置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"itcast" forKey:@"username"];
[defaults setFloat:18.0f forKey:@"text_size"];
[defaults setBool:YES forKey:@"auto_login"];
讀取上次保存的設(shè)置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *username = [defaults stringForKey:@"username"];
float textSize = [defaults floatForKey:@"text_size"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];
注意:UserDefaults設(shè)置數(shù)據(jù)時,不是立即寫入惯裕,而是根據(jù)時間戳定時地把緩存中的數(shù)據(jù)寫入本地磁盤温数。所以調(diào)用了set方法之后數(shù)據(jù)有可能還沒有寫入磁盤應(yīng)用程序就終止了。出現(xiàn)以上問題蜻势,可以通過調(diào)用synchornize方法強(qiáng)制寫入
[defaults synchornize];