如何写一个FMDB帮助类?看看runtime吧
FMDB是一个封装很好的sqllite类库。项目中调用的时候只需要写SQL语句,就能实现数据的CURD。我试过即使手写SQL语句也很麻烦,需要一个字段一个字段的拼上去,而且容易出错。有没有动态获取字段的一种做法呢。当然是有的。在.NET中就有获取一个类的每个字段名称和类型之类的方法。同理,我想OC中肯定也会存在,于是乎,强大的runtime机制就可以拿来用用了。
为什么用动态的呢,因为动态的拼接表面上就和ORM差不多了,开发者基本不用接触SQL语句,就能实现与数据库的数据交互。下面看具体介绍:
Class c = [SomeModel Class]; unsigned int outCount,i;
//class_copyPropertyList方法获取 SomeModel中的字段集合 objc_property_t *properties = class_copyPropertyList(c, &outCount);
然后遍历properties,获取到每个字段的字段名称和字段类型
objc_property_t property = properties[i]; propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
propertyType = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
这样,类中的每个字段都有了,类型也有了,那么拼接update语句或者insert语句再或者查询都可以。例如: UPDATE TABLE SET A='a',B='b',C=1 WHERE 1=1
字段A,B,C已经能动态取出,a,b,1可以根据新的Model 用 [model objectForKey:]方法获取值。先简单说这么多,下面我通过一个小Demo来演示一下。
首先实现方法,分析类的字段属性和类型,并存放到一个NSdictionary中。
- (NSDictionary *)pz_getClassPropertyWithClass:(Class)c { //存储列名和列类型的可变数组 NSMutableArray *propertyNames = [NSMutableArray array]; NSMutableArray *propertyTypes = [NSMutableArray array]; unsigned int outCount,i; objc_property_t *properties = class_copyPropertyList(c, &outCount); //属性名称 NSString *propertyName; NSString *propertyType; for (i = 0; i < outCount; i ++) { objc_property_t property = properties[i]; propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; //将不需要的排除 if ([_cloumnsNotInDB containsObject:propertyName]) { continue; } [propertyNames addObject:propertyName]; //获取属性类型 propertyType = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; if ([propertyType hasPrefix:@"T@"]) { [propertyTypes addObject:SQLTEXT]; }else if ([propertyType hasPrefix:@"Ti"]||[propertyType hasPrefix:@"TI"]||[propertyType hasPrefix:@"Ts"]||[propertyType hasPrefix:@"TS"]||[propertyType hasPrefix:@"TB"]) { [propertyTypes addObject:SQLINTEGER]; } else { [propertyTypes addObject:SQLREAL]; } } free(properties); return [NSDictionary dictionaryWithObjectsAndKeys:propertyNames,propertyNameKey,propertyTypes,propertyTypeKey, nil]; }
然后随便写个Model,调用方法如下:
NSDictionary *dict = [[PZFMDBUtil sharedUtil] pz_getClassPropertyWithClass:[PZDBModel class]]; NSLog(@"%@",dict);
打印结果为:
字段名称和类型都有啦,那么拼接 insert into table (name,age,number,address) values (?,?,?,?) 是不是很easy了呢。然后在调用一下FMDB的方法,就轻松实现Model直接保存或者更新到SQLLITE中了。
例如以下代码:
PZDBModel *model = [[PZDBModel alloc] init]; model.name = @"panzi"; model.number = 12; [[PZFMDBUtil sharedUtil] pz_addDataWithModel:model];
我们在看一下数据库:
我想,更新查询神马的就不用介绍了吧。当然呢,想在封装一层也是很麻烦的,要考虑好多东西。有时候想想还不如直接写SQL来的爽快~~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?