数据存储简单了解(NSUserDefaults)

数据存储-使用NSUserDefaults

两个类介绍:

NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类的,个人觉得使用NSUserDefaults是首选。因为如果使用自己建立的plist文件什么的,还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefaults则不用管这些东西,就像读字符串一样,直接读取就可以了。

NSUserDefaults支持的数据格式有:NSNumberIntegerFloatDouble),NSStringNSDateNSArrayNSDictionaryBOOL类型

NSKeyedUnarchiver类用于从文件(或者NSData指针)反归档模型。使用NSKeyedUnarchiver类来对数据进行归档和反归档。


一些函数介绍:

[NSKeyedUnarchiver unarchiveObjectWithData:data]//解码数据
[NSKeyedArchiver archivedDataWithRootObject:testObject];//编码数据

[[NSUserDefaults standardUserDefaults] objectForKey:key];//提取数据
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];删除数据
[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];//储存数据
[dic writeToFile:plistPath atomically:YES]//将数据保存到文件
[NSDictionary dictionaryWithContentsOfFile:plistPath];//从文件中提取数据

用例子具体讲解数据存储

创建一个自定义类,并将这个类中的数据用NSUserDefault存放起来

让这个自定义类实现<NSCoding>协议中的- (id) initWithCoder: (NSCoder *)coder方法和- (void) encodeWithCoder: (NSCoder *)coder方法,然后把该自定义的类对象编码到NSData中(使用NSKeyedUnarchiver归档),再从NSUserDefaults中进行读取。

函数介绍:

encodeWithCoder:
//每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey方法归档实例变量。

initWithCoder:
//每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用encodeObject:forKey:方法解码实例变量。

代码如下:

自定义类编写代码:

#import <Foundation/Foundation.h>

@interface cardTest : NSObject

@property (nonatomic, assign) int testInt;
@property (nonatomic, strong) NSArray *testArray;

@end

#import "cardTest.h"

@interface cardTest ()<NSCoding>

@end

@implementation cardTest

//解码函数
-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.testArray = [aDecoder decodeObjectForKey:@"testArray"];
        self.testInt = [aDecoder decodeIntForKey:@"testInt"];
    }
    return self;
}

//编码函数
-(void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.testArray forKey:@"testArray"];
    [aCoder encodeInt:self.testInt forKey:@"testInt"];
}
@end

储存类中的代码:

储存操作在- (void)viewDidLoad函数中进行

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end


#import "ViewController.h"
#import "cardTest.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    初始化
    cardTest *testObject = [[cardTest alloc] init];
    testObject.testInt = 8;
    testObject.testArray = [NSArray arrayWithObjects:@"dslajjfds", @"aslkdfjasksdkfjasl", nil];
    
//    压缩数据
    NSData *date = [NSKeyedArchiver archivedDataWithRootObject:testObject];
    
//    用NSUserDefaults储存数据
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:date forKey:@"cardTest_date"];
    
    NSData *newdate;
    cardTest *testObjectnew = [[cardTest alloc] init];
    
//    用NSUserDefaults获取数据
    newdate = [ud objectForKey:@"cardTest_date"];
//    解压数据
    testObjectnew= [NSKeyedUnarchiver unarchiveObjectWithData:newdate];
    
//    输出结果
    NSLog(@"%d", testObjectnew.testInt);
    NSLog(@"testArray = %@", testObjectnew.testArray);
    
}

@end

原文地址:https://www.cnblogs.com/AbeDay/p/5026952.html