SDUserDefaults:存储用户信息太痛苦?用这个就够了~

image.png

前言 寻ios马甲包技术大大,上架cp qp应用 Q 51259559 长期合作

先讲一下为什么要去封装这个单例类.一开始我是怎么进行数据的存储的?写一个单例然后添加属性,修改属性的Set方法,为了防止手写失误,还要定义宏常量.在删除的时候,不但要把属性置为nil,还要把NSUserDefaults的值置空,相当繁琐复杂,每增加一个属性就要增加最少十行代码.非常不利于管理.

在没有封装SDUserDefaults,多少在深夜中惊醒,生怕因为自己的疏忽,导致一些用户数据存储不上,可真是往事不堪回首呐,所以利用自己的一个空闲时间,封装一个SDUserDefaults存储单例,省去繁琐的步骤.使用起来简单粗暴.下面我们就来看一下如何使用SDUserDefaults这个单例类进行用户数据存储.


SDUserDefaults 使用

1.先去Github的SDUserDefaults下载演示Demo以及SDUserDefaults.

2.把SDUserDefaults文件夹导入你自己的项目合适位置,文件夹中主要包含SDUserDefaults和SDCodingObject两个类.

image.png

3.在SDUserDefaults的.h文件中添加你想要存储的属性,这里需要注意的是属性必须是遵循NSCoding协议的类,Foundation中的类都已经遵循该协议.如下图所示.

image.png

这时候有人会问,那我自定义的类需要怎么办?难道我需要自己实现NSCoding协议中的– (void)encodeWithCoder– (instancetype)initWithCoder方法吗?完全不需要!你需要继承于SDCodingObject这个类即可,我在其中都做了NSCoding协议的实现,并且所有的属性都会进行归档操作.例如上图的TestModel类.代码如下所示.

image.png

4.存储数据:只需要我们把对应的属性进行赋值,然后调用saveUserInfoAction方法即可.代码如下所示.

   [SDUserDefaults standardUserDefaults].name = @"用户数据";    TextModel *testModel = [[TextModel alloc] init];    testModel.name = @"骚栋";    testModel.age = @(15);    testModel.location = @"北京";    [SDUserDefaults standardUserDefaults].testModel = testModel;    [[SDUserDefaults standardUserDefaults] saveUserInfoAction]; // 存储数据

5.获取数据:直接取值就好,简单粗暴,没有任何问题.代码如下所示.

    /*****获取数据*****/    NSLog(@"%@",[SDUserDefaults standardUserDefaults].name);    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.name);    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.age);    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.location);

6.删除数据:想要删除数据直接调用deleteUserInfo即可.

   [[SDUserDefaults standardUserDefaults] deleteUserInfo];

7.更新数据:想要删除的话,就把那个属性置为nil,想要修改某个属性就把那个属性修改,最后调用saveUserInfoAction方法保存即可即可.

    [SDUserDefaults standardUserDefaults].name = @"新的用户数据";    [SDUserDefaults standardUserDefaults].testModel.location = nil;    [[SDUserDefaults standardUserDefaults] saveUserInfoAction]; // 更新数据

经过上面的步骤,我们就知道了如何使用SDUserDefaults,是不是非常的简单,接下来,我们看一下SDUserDefaults是如何实现的,只有了解原理,你才能在此基础上更好的定制自己想要的效果.

SDUserDefaults 实现


如何实现 SDUserDefaults的呢?主要用到了以下三点,分别是runtime,NSCoding协议,NSUserDefaults.

先说一下NSUserDefaults在其中扮演的角色,虽然NSUserDefaults还是作为存储空间使用,但是已经不单单是每一个单例属性都要进行一遍操作,因为SDUserDefaults已经遵循了NSCoding协议,所以我们可以直接进行归档存储,代码如下所示.

- (void)saveUserInfoAction {        NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:self];    [[NSUserDefaults standardUserDefaults] setObject:userInfoData forKey:SD_USER_MANAGER];}

那么什么时候进行从NSUserDefaults中进行取值呢?那就在单例创建的时候,判断一下NSUserDefaults是否含有归档数据,如果有,则进行归档,没有则进行空白初始化,代码如下所示.

static SDUserDefaults *userDefaults = nil;+ (instancetype)standardUserDefaults {        static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        if (userDefaults == nil) {            userDefaults = [SDUserDefaults initUserInfoAction];        }    });        return userDefaults;}+ (instancetype)initUserInfoAction {        NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:SD_USER_MANAGER];    if (userInfoData == nil) {        return [[SDUserDefaults alloc] init];    } else {        return [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];    }}

再说一下runtimeNSCoding协议的配合使用,这个主要是在SDCodingObject中进行了实现,利用runtime的方法遍历出所有的属性以及属性值,然后进行归档和解档操作.代码如下所示.

#pragma mark - 归档与解档- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {        unsigned int propertyCount = 0;    objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);    for (int i = 0; i < propertyCount; i++) {        objc_property_t *thisProperty = &propertyList[i];        const char *name = property_getName(*thisProperty);        NSString *propertyName = [NSString stringWithFormat:@"%s",name];        id propertyValue = [self valueForKey:propertyName];        [aCoder encodeObject:propertyValue forKey:propertyName];    }    free(propertyList);}- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {        if (self = [super init]) {                unsigned int propertyCount = 0;        objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);        for (int i = 0; i < propertyCount; i++) {            objc_property_t *thisProperty = &propertyList[i];            const char *name = property_getName(*thisProperty);            NSString *propertyName = [NSString stringWithFormat:@"%s",name];            [self setValue:[aDecoder decodeObjectForKey:propertyName] forKey:propertyName];        }        free(propertyList);    }    return self;}

当然了,我怕有些人不知道什么类没有遵循NSCoding协议,所以我在SDCodingObject初始化的时候就会检测所有的属性是否遵循了NSCoding协议,如果没有就会直接抛出异常,让老铁们能快速知道是什么属性没有遵循协议,方便问题的查找,代码如下所示.

// 检测所有成员变量是否遵循NSCoding协议- (void)testPropertyConformsToNSCodingProtocol {        unsigned int propertyCount = 0;    objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);    for (int i = 0; i  1) {            NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)];  //turns @"NSDate" into NSDate            Class typeClass = NSClassFromString(typeClassName);                        BOOL isConforms = [typeClass conformsToProtocol:@protocol(NSCoding)];            if (!isConforms) {                NSString *exceptionContent = [NSString stringWithFormat:@"%@ 类中的 %@属性 未遵循NSCoding协议,请手动调整",NSStringFromClass([self class]),typeClassName];                @throw [NSException exceptionWithName:@"property has not NSCoding Protocol" reason:exceptionContent userInfo:nil];            }        }    }    free(propertyList);}

当然了,在删除用户数据的时候也是用到了runtime,这样是非常方便的.代码如下所示.

- (void)deleteUserInfo {        unsigned int propertyCount = 0;    objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);    for (int i = 0; i < propertyCount; i++) {        objc_property_t *thisProperty = &propertyList[i];        const char *name = property_getName(*thisProperty);        NSString *propertyName = [NSString stringWithFormat:@"%s",name];        [self setValue:nil forKey:propertyName];    }    free(propertyList);    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SD_USER_MANAGER];}

后语


SDUserDefaults算是一个小工具类吧,有需要的直接拿走不谢,最后再一次附上Github的传送门(能点个star就更好了~~哈哈),如果有任何问题,欢迎在评论区或者简信联系我,谢谢大家了.

→ SDUserDefaults的传送门

作者:神经骚栋  交流群号: 869685378   寻ios马甲包技术大大,上架cp qp应用 Q 51259559 长期合作

猜你喜欢

转载自blog.csdn.net/ios8988/article/details/89477947