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

时间:2014-05-09 01:02:22   收藏:0   阅读:373

如下图:

bubuko.com,布布扣

从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,,
,我这边是已经在其他地方将数据表的字段名转化味驼峰式命名方式的。

将数据库select出来的数据转化为与相应databean对应的字典,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!