数据存储+归档

 //获取目录(两种方法)

    NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES) lastObject];

    NSLog(@"%@",directory);

//第二种方法

//  NSString *homeDir = NSHomeDirectory();

//  NSString *dir2 = [homeDir stringByAppendingPathComponent:@"Document"];

    

    NSArray *arry = @[@"xxx",@123];

    NSDictionary *dictionary = @{@"keyOne":@"string",@"keyTwo":@111111 };

    

   NSString *filePath1 = [directory stringByAppendingPathComponent:@"arrar.plist"];

    NSString *filePath2 = [directory stringByAppendingPathComponent:@"directionary.plist"];

    

   NSFileManager *fileManager = [[NSFileManager alloc]init];

    //移除

    //[fileManager removeItemAtPath:filePath1 error:nil];

    NSLog(@"%d", [fileManager fileExistsAtPath:filePath1]);

    

    //如果如果文件夹存在就写入(这里没有判断直接写进去的)

    [arry writeToFile:filePath1 atomically:YES];

   [dictionary writeToFile:filePath2 atomically:YES];

    

    

    //读取数据

    NSArray *readArray = [NSArray arrayWithContentsOfFile:filePath1];

    NSDictionary *readDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath2];

    NSLog(@"%@",readArray);

    NSLog(@"%@",readDictionary);

    

    //写入偏好设置

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    //存储数据

    [defaults setObject:@"hzt" forKey:@"string"];

    [defaults setBool:YES forKey:@"bool"];

    [defaults setInteger:12 forKey:@"int"];

    //立即同步

    [defaults synchronize];

    NSString *readString = [defaults objectForKey:@"string"];

    BOOL readBool = [defaults objectForKey:@"bool"];

    NSInteger readInterger = [defaults integerForKey:@"int"];

    

    NSLog(@"%@,\n%d,\n%lu",readString,readBool,readInterger);

 

//归档-------------------------------------------------------------------------------------------------------------------------

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES) lastObject];

    NSLog(@"%@",directory);

    

    Person *person = [[Person alloc]init];

    person.name = @"hzt";

    person.age = 21;

    

    NSString *path = [directory stringByAppendingPathComponent:@"person.plist"];

    //将对象归档

    [NSKeyedArchiver archiveRootObject:person toFile:path];

    

    //解档

    Person *personTwo = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    NSLog(@"%@,%d",personTwo.name,personTwo.age);

    

    

    //将对象保存到default

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:person];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:data forKey:@"person"];

    [defaults synchronize];//立即同步

    

    //取出

    NSData *readData = [defaults objectForKey:@"person"];

    Person *personThree = [NSKeyedUnarchiver unarchiveObjectWithData:readData];

    NSLog(@"%@,%d",personThree.name,personThree.age);

 

----------------------------------------归档对象(Model)需遵守协议(NSCoding)-------------------------------------------------

#import <Foundation/Foundation.h>

 

@interface Person : NSObject <NSCoding>

 

@property(nonatomic,copy) NSString *name;

@property(nonatomic,assign) int age;

 

- (void)encodeWithCoder:(NSCoder *)aCoder;

- (instancetype)initWithCoder:(NSCoder *)aDecoder;

 

@end

 

#import "Person.h"

 

@implementation Person

 

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.name forKey:@"name"];

    [aCoder encodeInt:self.age forKey:@"age"];

}

 

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self == [super init]) {

        self.name = [aDecoder decodeObjectForKey:@"name"];

       self.age = [aDecoder decodeIntForKey:@"age"];

    }

    return self;

}

 

 

 

 

转载于:https://www.cnblogs.com/sevenyears/p/5864480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值