我們想要長久保存自定義對象數(shù)據(jù)莫辨,那么就少不了對沙盒的操作洪灯。
獲得路徑的方式 Documents , Library , tmp
Documents : iTunes 會備份,蘋果也不允許保存東西在這里面白群。會被拒
tmp : 臨時保存,隨時可能被刪除
Library : ----> 下面2個子目錄
Caches :緩存文件
Preferences:偏好設(shè)置 保存賬號等信息 --> 經(jīng)過我測試 [NSUserDefaults standardUserDefaults] 就是保存在這里的
//獲取Caches路徑
NSArray *path1 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//獲取Library路徑
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//獲取Documents路徑
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
關(guān)于歸檔具體實現(xiàn)1尚胞,自定義對象 2, 數(shù)組帜慢,字典的歸檔
例子1:字典數(shù)組的歸檔
/**
字典的存取 沙盒路徑
*/
-(void)dicArchivie
{
NSString *chahe = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [chahe stringByAppendingString:@"accont.plist"];
NSDictionary *dic = @{@"name":@"xiaoming",@"age":@"18"};
//存
[dic writeToFile:path atomically:YES];
NSLog(@"paths = %@",path);
//取
NSDictionary *dicM = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dicM = %@",dicM);
}
/*
@interface NSDictionary<__covariant KeyType, __covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
這是字典的歸檔笼裳,直接歸檔到沙盒 cache路徑文件下面
從這里可以看出 ,要遵循 NSCoding,NSCopying
*/
例子2:自定義對象的歸檔解檔粱玲,我們歸檔一個person類躬柬。
//這是person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding,NSCopying>
@property (nonatomic,copy)NSString *name ;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,assign)CGFloat weight;
@end
//這是person.m 實現(xiàn)
#import "Person.h"
#import <objc/runtime.h>
@interface Person ()
@end
@implementation Person
//方式 1 ,runtime 快速歸檔
#if 0
//這是runtime 的方法 NSCoding 實現(xiàn)歸檔的協(xié)議
-(void)encodeWithCoder:(NSCoder *)aCoder
{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([self class], &count); //獲取所有的屬性
for (int i = 0; i<count; i++) {
//取出變量
Ivar ivar = ivars[i];
//查看變量的名字
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
//查看變量的類型
//NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// 利用KVC取出屬性對應(yīng)的值
id value = [self valueForKey:name];
//實現(xiàn)歸檔
[aCoder encodeObject:value forKey:name];
}
// 記住C語言中copy出來的要進(jìn)行釋放
free(ivars);
}
//解檔
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self =[super init]) {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([self class], &count);
for (int i = 0; i<count; i++) {
//取出變量
Ivar ivar = ivars[i];
//查看變量的名字
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
//取出變量的值
id value = [aDecoder decodeObjectForKey:name];
//KVC 賦值
[self setValue:value forKeyPath:name];
}
free(ivars);
}
return self;
}
#endif
//---------------------------------------------------------------------------
//方式2
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.age forKey:@"age"];
[aCoder encodeObject:@(self.weight) forKey:@"weight"];
}
//解檔
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self =[super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeObjectForKey:@"age"];
self.weight = [[aDecoder decodeObjectForKey:@"weight"] doubleValue];
}
return self;
}
//這是ViewController 調(diào)用方法
/**
自定義對象的沙盒存取 NSKeyedArchiver 必須用這個
*/
-(void)customObject
{
Person *p = [Person new];
p.name = @"小明";
p.age = @"14";
p.weight = 55;
NSString *chahe = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [chahe stringByAppendingString:@"accton.archive"];
NSLog(@"path = %@",path) ;
//自定義對象的存儲必須用 NSKeyedArchiver 不能用 writeToFile
//writeToFile適用 NSArray 和 NSDictionary
[NSKeyedArchiver archiveRootObject:p toFile:path]; //自定義對象 沙盒 存
Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];// 自定義對象 沙盒 取
NSLog(@"%@ %@ %f" ,person.name,person.age,person.weight);
//這里copy Person 必須實現(xiàn)NSCoying
// Person *p1 = [Person mutableCopy];
// NSLog(@"p1.name = %@" ,p1.name);
}