iOS的存储方式

plist存储方式

存储plist文件

NSArray *dataArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];

NSString *home = NSHomeDirectory();
NSLog(@"%@",home);

NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",documents);

//NSString *path = [NSString stringWithFormat:@"%@/data.plist",documents];
NSString *path = [documents stringByAppendingPathComponent:@"data.plist"];
NSLog(@"%@",path);

[dataArray writeToFile:path atomically:YES];

NSDictionary *dict = @{@"name":@"小明",@"age":@10};
[dict writeToFile:[documents stringByAppendingPathComponent:@"xiaoming.plist"] atomically:YES];

读取plist文件

NSString *home = NSHomeDirectory();
NSLog(@"%@",home);

NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",documents);

NSString *path = [documents stringByAppendingPathComponent:@"data.plist"];
NSLog(@"%@",path);

NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];

NSLog(@"dataArray:%@",dataArray);

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[documents stringByAppendingPathComponent:@"xiaoming.plist"]];

NSLog(@"%@",dict);

偏好设置存储方式

保存偏好设置

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"小明" forKey:@"name"];
[defaults setInteger:10 forKey:@"age"];
[defaults synchronize];

读取偏好设置

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%@",[defaults objectForKey:@"name"]);
NSLog(@"%zd",[defaults integerForKey:@"age"]);

可以使用偏好设置保存版本号,达到版本更新显示新特性页面

#define VERSION @"version"

...

NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:VERSION];

UIViewController *rootViewController;
if(lastVersion < currentVersion)
{
    //进入新特性界面
    rootViewController = [[ZFNewVersionViewController alloc] init];

    [[NSUserDefaults standardUserDefaults] setObject:[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"] forKey:VERSION];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
    //进入主框架
    rootViewController = [[ZFMainTabBarController alloc] init];
}

self.window.rootViewController = rootViewController;

归档存储方式

归档

由于plist或偏好设置(其实底层也是保存plist)无法保存对象类型的数据,这个时候就要用到归档

要使用归档存取一个类的对象,这个类必须先遵守NSCoding协议

比如创建一个Person类

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject <NSCoding>

@property(strong,nonatomic)NSString *name;
@property(assign,nonatomic)int age;

@end

并且实现- (void)encodeWithCoder:(NSCoder )aCoder的存入方法,- (instancetype)initWithCoder:(NSCoder )aDecoder的读取方法。如UIView之所以有initWithCoder方法,就是因为他已经遵守了NSCoding协议

Person.m

#import "Person.h"

@implementation Person

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    NSLog(@"name:%@  age:%d",self.name,self.age);
    [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;
}

@end

如果一个类中的某个属性是其他类的对象,那么这个其他类也同样需要遵守NSCoding

实现存取


//存入方法
- (IBAction)save:(id)sender {

    Person *person = [[Person alloc]init];
    person.name = @"Flashloft";
    person.age = 10;

    NSString *tmpPath = NSTemporaryDirectory();
    NSString *filePath = [tmpPath stringByAppendingPathComponent:@"Person.data"];

    [NSKeyedArchiver archiveRootObject:person toFile:filePath];

}

//读取方法
- (IBAction)read:(id)sender {

    NSString *tmpPath = NSTemporaryDirectory();
    NSString *filePath = [tmpPath stringByAppendingPathComponent:@"Person.data"];

    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"name:%@    age:%d",person.name,person.age);
}

results matching ""

    No results matching ""