ZJPermissionManager.m 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//
//  ZJPermissionManager.m
//  HDMall
//
//  Created by Javen on 2017/7/27.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "ZJPermissionManager.h"

@implementation ZJPermissionManager

- (void)updatePermissions:(NSArray *)permissions {
    //(1)获取类的属性及属性对应的类型
    NSMutableArray * keys = [NSMutableArray array];
    NSMutableArray * attributes = [NSMutableArray array];
    /*
     * 例子
     * name = value3 attribute = T@"NSString",C,N,V_value3
     * name = value4 attribute = T^i,N,V_value4
     */
    unsigned int outCount;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    for (int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //通过property_getName函数获得属性的名字
        NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        [keys addObject:propertyName];
        //通过property_getAttributes函数可以获得属性的名字和@encode编码
        NSString * propertyAttribute = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
        [attributes addObject:propertyAttribute];
    }
    //立即释放properties指向的内存
    free(properties);
    
    for (NSString *string in permissions) {
        NSArray *first = [string componentsSeparatedByString:@":"];
        NSString *permissionKey = [first firstObject];
        
        NSString *permissionValue = [first objectAtIndex:1];
        NSArray *values = [permissionValue componentsSeparatedByString:@";"];
        
        if ([keys containsObject:permissionKey]) {
            
            ZJPermission *permission = [ZJPermission modelWithDict:values];
            [self setValue:permission forKey:permissionKey];
        }
    }
    
}

@end

@implementation ZJPermission


+ (instancetype)modelWithDict:(NSArray *)arr {
    ZJPermission *model = [[ZJPermission alloc] initWithDict:arr];
    return model;
}
- (instancetype)initWithDict:(NSArray *)arr {
    
    if (self = [self init]) {
        //(1)获取类的属性及属性对应的类型
        NSMutableArray * keys = [NSMutableArray array];
        NSMutableArray * attributes = [NSMutableArray array];
        /*
         * 例子
         * name = value3 attribute = T@"NSString",C,N,V_value3
         * name = value4 attribute = T^i,N,V_value4
         */
        unsigned int outCount;
        objc_property_t * properties = class_copyPropertyList([self class], &outCount);
        for (int i = 0; i < outCount; i ++) {
            objc_property_t property = properties[i];
            //通过property_getName函数获得属性的名字
            NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            [keys addObject:propertyName];
            //通过property_getAttributes函数可以获得属性的名字和@encode编码
            NSString * propertyAttribute = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
            [attributes addObject:propertyAttribute];
        }
        //立即释放properties指向的内存
        free(properties);
        
        //(2)根据类型给属性赋值
        for (NSString * key in keys) {
            if ([arr containsObject:key]) {
                [self setValue:[NSNumber numberWithBool:YES] forKey:key];
            }else if ([key isEqualToString:@"newField"] && [arr containsObject:@"new"]) {
                [self setValue:[NSNumber numberWithBool:YES] forKey:key];
            }else{
                [self setValue:[NSNumber numberWithBool:NO] forKey:key];
            }
            
        }
    }
    return self;
    
}
@end