首页 > 代码库 > 将数据库select出来的数据转化为与相应databean对应的字典

将数据库select出来的数据转化为与相应databean对应的字典

如下图:


从user_logs表格select出来的数据放在一个可变数组NSMutableArray中,如 user_logs,现在要

把数据一条条转化为Userlog databean,UserLog定义如下:
@interface UserLog : NSObject
@property(strong,nonatomic) NSString * logId;
@property(strong,nonatomic) NSString * userId;
@property(strong,nonatomic) NSString * actionType;
@property(strong,nonatomic) NSDate * actionDate;
@property(strong,nonatomic) NSString * accountNumber;
@property(strong,nonatomic) NSString * dataRef1;
@property(strong,nonatomic) NSString * dataRef2;
@property(strong,nonatomic) NSString * dataRef3;
@property(nonatomic) BOOL uploaded;
@property(nonatomic) NSInteger udts;
@property(strong,nonatomic) NSString * logType;


- (NSDictionary *)convertToDictionary;
@end

转化方法如下:
-(NSDictionary *)convertUserLogsPropertyToDictionary:(NSMutableArray *)userLogs{
    NSMutableArray  *userLogsMutableArray = [NSMutableArray array];
    for (int i = 0;i< [userLogs count];i++) {
        UserLog *userLog = userLogs[i];
        [userLogsMutableArray addObject:[userLog convertToDictionary]];
    }
    NSDictionary *userLogsDictionary = @{@"objects":userLogsMutableArray};
    return userLogsDictionary;
}

注意:上面databean的属性并没有与user_logs表中的字段名相同,转化过来的字典会是nil,,
,我这边是已经在其他地方将数据表的字段名转化味驼峰式命名方式的。