美文网首页
【iOS】关于单例类中清空自身属性

【iOS】关于单例类中清空自身属性

作者: 雨声不吃鱼 | 来源:发表于2018-03-05 09:37 被阅读0次
  • 最近做项目中,遇到这样一个问题:用户信息存在一个单例中,当用户退出登录时,要清空整个用户的属性,本文使用了如下的解决方案
/**
 清空属性值
 */
- (void)cleanWithAllProperties {
    unsigned int pro_count = 0;
    // 获取该类中所有属性列表
    objc_property_t *properties = class_copyPropertyList([self class], &pro_count);
    // for循环遍历所有属性
    for (int i = 0; i < pro_count; i ++) {
        objc_property_t property = properties[i];
        // 得到当前属性的名字(字符串形式)
        NSString *propertyName = [NSString stringWithFormat:@"%s", property_getName(property)];
        // 使用KVC方式得到该属性的值
        id propertyValue = [self valueForKey:(NSString *)propertyName];
        
        // null的就不用管了
        if (!propertyValue ||
            [propertyValue isKindOfClass:[NSNull class]]) {
            continue;
        }
        
        // !!!:同样通过KVC的方式赋值

        if ([propertyValue isKindOfClass:[NSString class]]) {
            // 字符串类型
            [self setValue:@"" forKey:propertyName];
            NSLog(@"--> 清理用户信息[%@]成功 NSString:%@",propertyName,propertyValue);
        }
        else if ([propertyValue isKindOfClass:[NSNumber class]]) {
            // bool int float long ...
            [self setValue:[NSNumber numberWithInteger:0] forKey:propertyName];
            NSLog(@"--> 清理用户信息[%@]成功 NSNumber:%@",propertyName,propertyValue);
        }
        else if ([propertyValue isKindOfClass:[NSMutableDictionary class]] ||
                 [propertyValue isKindOfClass:[NSDictionary class]]) {
            // 字典
            [self setValue:@{} forKey:propertyName];
            NSLog(@"--> 清理用户信息[%@]成功 NSDictionary:%@",propertyName,propertyValue);
        }
        else if ([propertyValue isKindOfClass:[NSMutableArray class]] ||
                 [propertyValue isKindOfClass:[NSArray class]]) {
            // 数组
            [self setValue:@[] forKey:propertyName];
            NSLog(@"--> 清理用户信息[%@]成功 NSArray:%@",propertyName,propertyValue);
        }
        else {
            // 其他未知类型 包括data
            // 这里还可以增加其他判断...
            [self setValue:nil forKey:propertyName];
            NSLog(@"--> 清理用户信息[%@]成功 其他未知类型:%@",propertyName,propertyValue);
        }
    }
    // 释放
    free(properties);
    
    /*
    // 置空父类(PowerStationForHouseholdModel)的属性值
    pro_count = 0;
    objc_property_t *properties_super = class_copyPropertyList([self superclass], &pro_count);
    for (int i = 0; i < pro_count; i ++) {
        objc_property_t property = properties_super[i];
        NSString *propertyName = [NSString stringWithFormat:@"%s", property_getName(property)];
        // 可以自己根据要求修改
        [self setValue:nil forKey:propertyName];
    }
    free(properties_super);
     */
}

// 如果属性和字典中的key不一致,可以重写此方法 / 或者readonly
// 不一致的key和对应的value都会通过这个方法返回,可以在此方法中做特殊处理
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"-------> forUndefinedKey:%@  value:%@",key,value);
}

拓展:可以通过这种思路拓展到一键设置属性,方便管理,等等

相关文章

  • 【iOS】关于单例类中清空自身属性

    最近做项目中,遇到这样一个问题:用户信息存在一个单例中,当用户退出登录时,要清空整个用户的属性,本文使用了如下的解...

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS - 清空单例

    iOS开发时有时候我们需要清空单例:

  • 十一.NSUserDefaults本地保存 移除

    NSUserDefaults是iOS系统提供的一个单例类(iOS提供了若干个单例类),通过类方法standardU...

  • @sychronized和dispatch_once,以及对单例

    在iOS开发中,经常使用到单例。单例是Cocoa中被广泛使用的设计模式之一。单例使得某个类在整个applicati...

  • iOS单例类的创建和简单使用

    单例类的两种创建方式: .h文件 .m文件 在其他类中使用单例类中的属性(比如字符串:@“900”)。先在该类中导...

  • ios 开发中的单例模式

    其实iOS开发中的单例模式无非就是一个类创建的对象在程序中只有一个对象! iOS中的单例模式有分为赖汉式和饿汉式单...

  • 利用Runtime清空单例属性

    但是上面代码有一个重大bug: 当对象里面的属性不是对象属性时(例如NSInteger CGFloat等),程序会...

  • 单例singleton模式–单例类的实现

    单例模式的作用是用来解决一个应用中某个类只有唯一一个对象的问题。单例类在iOS开发中是非常重要的,在系统提供个类中...

  • iOS单例模式

    单例模式 解决“应用中只有一个单例”的一类问题。 Objecttive-C实现原理 单例模式一般会封装一个静态属性...

网友评论

      本文标题:【iOS】关于单例类中清空自身属性

      本文链接:https://www.haomeiwen.com/subject/whmcfftx.html