美文网首页
iOS 数据存储

iOS 数据存储

作者: 青椒辣不辣 | 来源:发表于2020-12-21 15:31 被阅读0次

\color{rgb(0 ,139, 139)}{路径获取}

    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同步设备时会备份该目录

\color{rgb(0 ,139, 139)}{plist存储}

/*
 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存储

\color{rgb(0 ,139, 139)}{偏好设置NSUserDefaults}

-(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

\color{rgb(0 ,139, 139)}{归档存储NSKeyedArchiver}

-(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

\color{rgb(0 ,139, 139)}{}


相关文章

  • iOS数据存储

    iOS数据存储 数据存储

  • iOS数据库存储之SQLite3

    资源连接: iOS数据库存储之SQL语句; iOS数据存储之文件沙盒; iOS数据存储之NSCoding; SQL...

  • iOS数据存储之文件沙盒

    资源连接: iOS数据库存储之SQL语句; iOS数据库存储之SQLite3; iOS数据存储之NSCoding;...

  • iOS数据存储之NSCoder

    资源连接: iOS数据库存储之SQL语句; iOS数据库存储之SQLite3; iOS数据存储之文件沙盒; iOS...

  • iOS数据库存储之SQL语句

    资源连接: iOS数据库存储之SQLite3; iOS数据存储之文件沙盒; iOS数据存储之NSCoding; S...

  • iOS-12 数据存储

    1iOS数据存储之Core Data(一) - 简书 2iOS数据存储之归档 - 简书 3iOS数据存储之偏好设置...

  • iOS项目中CoreData的使用

    一、iOS数据储存方式 二、简介CoreData数据存储Core Data 是 iOS系统提供的数据存储方式,和传...

  • iOS数据存储--数据库

    iOS中数据存储方式 iOS中数据存储方式主要有以下几种 Plist NSUserDefaults NSCodin...

  • 沙盒机制

    IOS数据存储之文件沙盒存储

  • iOS基础之数据存储

    iOS数据存储 1. 概论 在iOS开发中数据存储的方式可以归纳为两类: 存储文件 和 存储到数据库. 2.文件存...

网友评论

      本文标题:iOS 数据存储

      本文链接:https://www.haomeiwen.com/subject/xnlfnktx.html