iOS Model 保存到数据库的半小时简单设计

demo:

CClass* cClass = [CClass new];
cClass.theO = @"7";
cClass.theP = @"8";
cClass.theQ = @"9";
 
BClass* bClass = [BClass new];
bClass.theH = @"4";
bClass.theI = @"5";
bClass.theJ = @"6";
bClass.cClass = cClass;
 
AClass* aClass = [AClass new];
aClass.theA = @"1";
//aClass._theB = @"2";
aClass.theC = @"3";
aClass.bClass = bClass;

//对象生成表
[self db:db objectToTaleWithClassString:NSStringFromClass([aClass class])];

//保存数据
[self db:db tableContentFromObject:aClass];

具体实现:

- (void) db:(QSDataBase*)db objectToTaleWithClassString:(NSString*)classString {
    NSMutableArray* keepField = [NSMutableArray array];
    NSArray* tempA = [MZCRuntimeUtils attributesOfPropertiesModelFromClass:NSClassFromString(classString)];
    for (MZCRuntimeUtilsPropertiesAttributes* mm in tempA) {
        if ([mm.classString isEqualToString:@"NSString"]) {
            [keepField addObject:mm.propertyString];
        } else {
            [keepField addObject:mm.classString];
            
            [self db:db objectToTaleWithClassString:mm.classString];
        }
    }
    [db createTableIfNotExistsWithName:classString withFields:keepField];
}
- (void) db:(QSDataBase*)db tableContentFromObject:(NSObject*)object {
    NSMutableArray* keepField = [NSMutableArray array];
    NSMutableArray* keepValue = [NSMutableArray array];
    
    NSArray* tempA = [MZCRuntimeUtils attributesOfPropertiesModelFromClass:[object class]];
    for (MZCRuntimeUtilsPropertiesAttributes* mm in tempA) {
        
        if ([mm.classString isEqualToString:@"NSString"]) {
            id temp = [object valueForKey:mm.propertyString];
            if (temp) {
                [keepField addObject:mm.propertyString];
                [keepValue addObject:temp];
            }
        } else {
            id temp = [object valueForKey:mm.propertyString];
            if (temp) {
                NSArray* tempB = [MZCRuntimeUtils attributesOfPropertiesModelFromClass:[temp class]];
                if ([tempB isKindOfClass:[NSArray class]] && [tempB count] > 0) {
                    MZCRuntimeUtilsPropertiesAttributes* mm2 = [tempB objectAtIndex:0];
                    if ([mm2.classString isEqualToString:@"NSString"]) {
                        id temp2 = [temp valueForKey:mm2.propertyString];
                        if (temp2) {
                            [keepField addObject:mm.classString];
                            [keepValue addObject:temp2];
                        }
                    }
                }
                [self db:db tableContentFromObject:temp];
            }
        }
    }
    [db insertIntoTableWithName:NSStringFromClass([object class]) withValues:keepValue forFields:keepField];
}

数据库:
6E698A6DDE83E27827D9A761B011A334
11377B5F7FEB09C48912AF0F554F17E2
703C0865009D0D9C98233EB2E5BE9254

类:

#import <Foundation/Foundation.h>
#import "BClass.h"

@interface AClass : NSObject


@property(nonatomic,strong) NSString* theA;
@property(nonatomic,strong) NSString* _theB;
@property(nonatomic,strong) NSString* theC;

@property(nonatomic,strong) BClass* bClass;

@end
#import <Foundation/Foundation.h>
#import "CClass.h"

@interface BClass : NSObject

@property(nonatomic,strong) NSString* theH;
@property(nonatomic,strong) NSString* theI;
@property(nonatomic,strong) NSString* theJ;

@property(nonatomic,strong) CClass* cClass;

@end
#import <Foundation/Foundation.h>

@interface CClass : NSObject

@property(nonatomic,strong) NSString* theO;
@property(nonatomic,strong) NSString* theP;
@property(nonatomic,strong) NSString* theQ;

@end



PS: 为了达成关联的关系,第一个属性最好代表ID值,方便操作。
PS: 由于水平有限,大致写了半个小时,这个玩意。