NSString *path1 = NSHomeDirectory();
NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path3 = NSTemporaryDirectory();
NSString *path4 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *path5 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path6 = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Preferences"];
NSLog(@"%@",path1);//homePath
NSLog(@"%@",path2);//documentPath
NSLog(@"%@",path3);//temporaryPath
NSLog(@"%@",path4);//libraryPath
NSLog(@"%@",path5);//cachePath
NSLog(@"%@",path6);//preferencePath
baseUrl : /Users/jintian/Library/Developer/CoreSimulator/Devices/F958D05D-E649-4372-9BD4-A1B372EEDDB6
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB/Documents
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB/tmp/
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB/Library
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB/Library/Caches
baseUrl/data/Containers/Data/Application/5052D685-B5DB-4AAB-B4D4-F6B09806F6EB/Library/Preferences
存储路径
1. 应用程序包:(上图中的Layer)包含了所有的资源文件和可执行文件
2. Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
3. tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录
4. Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据
5. Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录
/*
writeToFile: 每次存储都会覆盖前边的所有数据
在plist文件当中, 不能存自定义的对象.
如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中
*/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; //获取指定的目录
NSString *filePath = [path stringByAppendingPathComponent:@"dict.plist"]; //拼接文件名
NSDictionary *dict = @{@"per" : @"GRJ", @"age" : @120, @"name" : @"OK"};
[dict writeToFile:filePath atomically:YES];
NSDictionary *dict3 = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@",dict3);
}
{
age = 120;
name = "OK ";
per = GRJ;
}
plist存储
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"grj" forKey:@"name"];//plist文件存储形式
[defaults setInteger:20 forKey:@"age"];
[defaults setBool:YES forKey:@"isBool"];
NSString *name = [defaults objectForKey:@"name"];
NSInteger age = [defaults integerForKey:@"age"];
BOOL isBool = [defaults boolForKey:@"isBool"];
NSLog(@"%@--%ld-%d",name,age,isBool);
}
grj--20-1
偏好设置NSUserDefaults
image.png
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
RJUserBaseInfoArchiver *userBaseInfo = [[RJUserBaseInfoArchiver alloc] init];
userBaseInfo.userName = @"grj";
userBaseInfo.telephoneNumber = @"15912341234";
userBaseInfo.age = 12;
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//文件名可以用.data|.archiver|.plist 等等...
NSString *filePath = [path stringByAppendingPathComponent:@"userBaseInfo.archiver"];
//底层会自动调用encodeWithCoder:方法,目的:问下要保存定义的对象哪些属性
[NSKeyedArchiver archiveRootObject:userBaseInfo toFile:filePath];
//unarchiveObjectWithFile:底层会自动调用initWithCoder:方法,问下,要读取对象哪些属性
RJUserBaseInfoArchiver *getUserBaseInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@---%d----%@",getUserBaseInfo.userName,getUserBaseInfo.age,getUserBaseInfo.telephoneNumber);
//NSString、NSDictionary、NSArray、NSData、NSNumber 等
NSString *path9 = [path stringByAppendingPathComponent:@"abc.archiver"];
[NSKeyedArchiver archiveRootObject:@"遵守<NSCoding>协议的系统类可直接存取" toFile:path9];
NSString *str = [NSKeyedUnarchiver unarchiveObjectWithFile:path9];
NSLog(@"%@", str);
}
grj---12----15912341234
遵守<NSCoding>协议的系统类可直接存取
@interface RJUserBaseInfoArchiver : NSObject<NSCoding>
@property (copy ,nonatomic) NSString *userName;
@property (copy ,nonatomic) NSString *telephoneNumber;
@property (assign,nonatomic) int age;
@end
--------------------------------------------------------
@implementation RJUserBaseInfoArchiver
-(void)encodeWithCoder:(NSCoder *)coder{
[coder encodeObject:self.userName forKey:@"userName"];
[coder encodeObject:self.telephoneNumber forKey:@"telephoneNumber"];
[coder encodeInt:self.age forKey:@"age"];
}
-(instancetype)initWithCoder:(NSCoder *)coder{
if (self = [super init]) {
self.userName = [coder decodeObjectForKey:@"userName"];
self.telephoneNumber = [coder decodeObjectForKey:@"telephoneNumber"];
self.age = [coder decodeIntForKey:@"age"];
}
return self;
}
@end
NSKeyedArchiver
网友评论