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

    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;

}