发现公司的项目中的数据不是从网络获取,而是一个创建好的.db文件,其中有很多表,就写了个转 model 的方法
创建单例,以便整个项目任何地方使用
// .h 文件中的代码
#import "DataBase.h"
static DataBase *_DBCtl = nil;
@interface DataBase(){
FMDatabase *_db;
}
@end
@implementation DataBase
#pragma mark - 初始化数据库
//创建数据库,并将数据库转成 model 存在数组中方便全局使用
- (void)creatDataBase {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DLDT" ofType:@"db"];
if(!_db){
_db = [FMDatabase databaseWithPath:filePath];
}
}
/**
将数据库转成model
@param table 数据库中表的名字
@param myClass model , 存储属性名对应数据库中的字段名
@return 装有model的数组
*/
- (NSMutableArray *)getTable:(NSString *)table fromClass:(NSObject *)myClass {
if (![_db open]) { return nil; }
//数据库数组
NSMutableArray *arrSQL = [[NSMutableArray alloc] init];
// 查询数据库
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@",table];
FMResultSet *res = [_db executeQuery:sql];
// 定义一个变量记录对应 model 的属性数量和属性名集合
unsigned int outCount;
while ([res next]) {
// 获取传入类的属性名
objc_property_t *propertys = class_copyPropertyList([myClass class], &outCount);
// 创建一个字典,用来存储数据库的 K-V
NSMutableDictionary *dicProperty = [NSMutableDictionary dictionary];
for (int i = 0; i < outCount; i ++) {
//获取属性名
objc_property_t property = propertys[i];
// 将属性名作为字典 key
NSString *strKey = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
// 用 key 去是数据库中获取值
NSString *strValue = [res stringForColumn:strKey];
// 去空
strValue = strValue == nil ? @"" : strValue;
[dicProperty setObject:strValue forKey:strKey]; //存值
}
// 释放属性
free(propertys);
// 转 model
myClass = [[myClass class] mj_objectWithKeyValues:dicProperty];
// 放入数组
[arrSQL addObject:myClass];
}
[_db close]; // 关闭数据库
return arrSQL; // 返回装有 model 的数组
}
@end
使用
//这是 .m 文件中的实现,用tableView 展示数据库美容
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"数据库转model";
_arrInfo = [[DataBase shareData] getTable:@"station" fromClass:[DataModel copy]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"station";
DataModelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[DataModelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
_model = _arrInfo[indexPath.row];
cell.label.text = _model.StationName;
return cell;
}
//下面是 swift 版
// 只要将 db 文件的引入,传入相应表名和 model 类,就可以得到相应model 了,
//将数据库转成 model
private func getTable(table : String , from className : NSObject.Type) -> (NSMutableArray) {
if db?.open() == false { return [] }
let MyClass : NSObject.Type? = className
let arrSQL = NSMutableArray()
let path = "SELECT * FROM \(table)"
let result = db?.executeQuery(path, withArgumentsIn: nil)
var outCount : UInt32 = 0
while (result?.next())! {
let properties = class_copyPropertyList(MyClass, &outCount)
let dic = NSMutableDictionary()
for index in 0 ..< numericCast(outCount) {
let property : objc_property_t = properties![index]!
let key : String = NSString.init(utf8String: property_getName(property)) as! String
let value = result?.string(forColumn: key)
//存字典
dic.setObject(value ?? "", forKey: key as NSCopying)
}
free(properties)
var myClass = MyClass?.init()
myClass = MyClass!.mj_object(withKeyValues: dic)
arrSQL.add(myClass!)
}
return arrSQL
}