iOS Model存储

#import 

NS_ASSUME_NONNULL_BEGIN

@interface Student : NSObject 

@property (nonatomic, copy) NSString * name;
@property (nonatomic, copy) NSString * studentNumber;
@property (nonatomic, copy) NSString * sex;

@end

NS_ASSUME_NONNULL_END
#import "Student.h"

@implementation Student

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.studentNumber forKey:@"studentNumber"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.studentNumber = [aDecoder decodeObjectForKey:@"studentNumber"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
    }
    return self;
}


@end
#define studentKey @"studentKey"

- (void)test {
    Student * student = [[Student alloc] init];
     
    student.name = @"liuyifei";
    student.studentNumber = @"100111";
    student.sex = @"女";

    
    NSDictionary * dic = [self dicFromObject:student];
    Student * student2 = [[Student alloc] init];
    [student2 setValuesForKeysWithDictionary:dic];
    
    NSLog(@"88=%@---%@", dic, student2.name);
    
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:student];
    
    NSMutableArray * array = [NSMutableArray array];
    [array addObject:data];

    for (int i = 0; i < 100; i++) {
        Student * student = [[Student alloc] init];
        student.name = [NSString stringWithFormat:@"nancy%d", i];
        student.studentNumber = [NSString stringWithFormat:@"%d", i];
        student.sex = i % 2 == 0 ? @"男" : @"女";

        NSData * data = [NSKeyedArchiver archivedDataWithRootObject:student];
        [array addObject:data];
    }

    
    NSArray * dataArray = array.copy;
    
    [[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:studentKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSArray * endArray = [[NSUserDefaults standardUserDefaults] objectForKey:studentKey];
    
    Student * stu = [NSKeyedUnarchiver unarchiveObjectWithData:endArray.firstObject];
    
    NSLog(@"99=%@--%@", endArray, stu.name);
}


//model转化为字典
- (NSDictionary *)dicFromObject:(NSObject *)object {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([object class], &count);
 
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [object valueForKey:name];//valueForKey返回的数字和字符串都是对象
 
        if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
            //string , bool, int ,NSinteger
            [dic setObject:value forKey:name];
 
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            //字典或字典
            [dic setObject:[self arrayOrDicWithObject:(NSArray*)value] forKey:name];
 
        } else if (value == nil) {
            //null
            //[dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????
 
        } else {
            //model
            [dic setObject:[self dicFromObject:value] forKey:name];
        }
    }
 
    return [dic copy];
}

//将可能存在model数组转化为普通数组
- (id)arrayOrDicWithObject:(id)origin {
    if ([origin isKindOfClass:[NSArray class]]) {
        //数组
        NSMutableArray *array = [NSMutableArray array];
        for (NSObject *object in origin) {
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [array addObject:object];
 
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [array addObject:[self arrayOrDicWithObject:(NSArray *)object]];
 
            } else {
                //model
                [array addObject:[self dicFromObject:object]];
            }
        }
 
        return [array copy];
 
    } else if ([origin isKindOfClass:[NSDictionary class]]) {
        //字典
        NSDictionary *originDic = (NSDictionary *)origin;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
 
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [dic setObject:object forKey:key];
 
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[self arrayOrDicWithObject:object] forKey:key];
 
            } else {
                //model
                [dic setObject:[self dicFromObject:object] forKey:key];
            }
        }
 
        return [dic copy];
    }
 
    return [NSNull null];
}

你可能感兴趣的:(iOS Model存储)