ios NSUserDefaults用法

本文简单介绍NSUserDefaults的存、取、移除用法,代码如下:

[[NSUserDefaults standardUserDefaults] setObject:@"exampleObject" forKey:@"exampleKey"];//存
[[NSUserDefaults standardUserDefaults] objectForKey:@"exampleKey"];//取
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"exampleKey"];//移除

使用NSUserDefaults存储除NSArray、NSData、NSDictionary、NSString、NSNumber等基本数据类型之外的自定义类型,报错如下:

//存储一个包含TradeRecordModel类型的Model的Array到NSUserDefaults中
Attempt to set a non-property-list object (
    "",
    "",
    ""
) as an NSUserDefaults/CFPreferences value for key myWalletTradeRecordList

要存储自定义类型的对象,用NSUserDefaults存储时要对其进行归档处理,取出时要先解档;
下面以NSUserDefaults存储自定义Model(TradeRecordModel)为例,代码如下:

/**
 *  TradeRecordModel.h
 */
@interface TradeRecordModel : NSObject  //首先遵守代理
@property (nonatomic,strong) NSString * ID;
@property (nonatomic,strong) NSString * title;
@property (nonatomic,strong) NSString * change;
@property (nonatomic,strong) NSString * created_at;
HMSingletonH(TradeRecordModel)//单例模式
@end
/**
 *  TradeRecordModel.m
 */
@implementation TradeRecordModel
HMSingletonM(TradeRecordModel)//单例模式
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"id"]) {
        _ID = value;
    }
}
#pragma mark NSCoding
//步骤二:实现NSCoding的两个代理
- (id)initWithCoder:(NSCoder *)aDecoder{
    
    if (self == [super init]) {
        _ID = [aDecoder decodeObjectForKey:@"id"];
        _title = [aDecoder decodeObjectForKey:@"title"];
        _change = [aDecoder decodeObjectForKey:@"change"];
        _created_at = [aDecoder decodeObjectForKey:@"created_at"];
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:_ID forKey:@"id"];
    [aCoder encodeObject:_title forKey:@"title"];
    [aCoder encodeObject:_change forKey:@"change"];
    [aCoder encodeObject:_created_at forKey:@"created_at"];
}
@end

这样model类的处理就告于段落了,在调用NSUserDefaults存储时,用下列代码进行处理:

TradeRecordModel *model = [TradeRecordModel new];
for (NSDictionary *dict in response) {
      [model setValuesForKeysWithDictionary:dict];//设置model
      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];//归档model
      NSLog(@"归档数据:%@",data)
      [arr addObject:data];                                                          
}
[[NSUserDefaults standardUserDefaults] setObject:arr forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];

model的设置,参考链接:http://www.jianshu.com/p/76d6d1c56d3b。其余问题请参考自身实际情况;
在调用NSUserDefaults取出数据时,代码如下:

NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:key];
TradeRecordModel *model = [TradeRecordModel new];
for (NSData *data in arr) {
    model = [NSKeyedUnarchiver unarchiveObjectWithData:data];//解档model
    NSLog(@"解档数据:%@",model.ID)
    [tradeRecordListArr addObject:model];
}

可变数组tradeRecordListArr中,就存储了你之前存储的那个model了。
此外,在此介绍一个NSUserDefaults的宏定义方法,代码:

//NSUserDefault缓存
#define SAVEDEFAULTS(value,key) [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];\
                                [[NSUserDefaults standardUserDefaults] synchronize];
#define GETDEFAULTS(key) [[NSUserDefaults standardUserDefaults] objectForKey:key];

The end!

你可能感兴趣的:(ios NSUserDefaults用法)