#import <objc/runtime.h>
typedef NS_ENUM(NSUInteger, PropertyArrayType) {
PropertyArrayTypeName,
PropertyArrayTypeAttribute
};
+ (NSMutableArray *)arrayWithModel:(id)model
type:(PropertyArrayType)type{
Class clazz = [model class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
NSMutableArray* attributeArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
objc_property_t prop= properties[i];
const char* propertyName = property_getName(prop);
const char* attributeName = property_getAttributes(prop);
[attributeArray addObject:[NSString stringWithCString:attributeName encoding:NSUTF8StringEncoding]];
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
// const char* attributeName = property_getAttributes(prop);
// NSLog(@"%@",[NSString stringWithUTF8String:propertyName]);
// NSLog(@"%@",[NSString stringWithUTF8String:attributeName]);
}
free(properties);
switch (type) {
case PropertyArrayTypeName:
return propertyArray;
break;
case PropertyArrayTypeAttribute:
return attributeArray;
break;
}
return nil;
}
- (NSMutableArray *)selectALWithModel:(id)model{
// NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// NSString *path = [sandBoxPath stringByAppendingPathComponent:@"Student.sqlite"];
// int result = sqlite3_open([path UTF8String], &dbPoint);
NSMutableArray *models = [[self class]arrayWithModel:model type:PropertyArrayTypeName];
NSMutableArray *modelAttributes = [[self class]arrayWithModel:model type:PropertyArrayTypeAttribute];
NSLog(@"%@", modelAttributes);
sqlite3_stmt *stmt = nil;
NSString *sqlStr = @"select * from stu";
int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
NSMutableArray *arr = [NSMutableArray array];
if (result == SQLITE_OK) {
NSLog(@"成功");
while (sqlite3_step(stmt) == SQLITE_ROW){
id temp = [model copy];
for (int i = 1; i <= models.count; i++) {
NSString *propName = models[i - 1];
NSString *attributeName = modelAttributes[i - 1];
SEL setterMothod = NSSelectorFromString([NSString stringWithFormat:@"set%@:", propName.capitalizedString]);
if ([attributeName hasPrefix:@"Tq"]) {
int age = sqlite3_column_int(stmt, i);
[temp performSelector:setterMothod withObject:(NSInteger)age];
} else if ([attributeName hasPrefix:@"T@\"NSString\""]){
const unsigned char *pro = sqlite3_column_text(stmt, i);
NSString *modelPro = [NSString stringWithUTF8String:(const char *)pro];
[temp performSelector:setterMothod withObject:modelPro];}
}
[arr addObject:temp];
}
}else {
NSLog(@"失败");
}
return arr;
}
@end
// 创建表格
NSString *sqlStr = @"create table if not exists stu(number integer primary key autoincrement, name text ,sex text, age integer, hobby text)";
// 增
NSString *sqlsStr = [NSString stringWithFormat:@"insert into stu (name, age , sex, hobby)\n values ('%@', '%ld', '%@', '%@')", stu.name, stu.age, stu.sex, stu.hobby];
// 改
NSString *sqlsStr = [NSString stringWithFormat:@"update stu set name = '%@', sex = '%@', hobby = '%@', age = '%ld' where name = '刘山山'", stu.name, stu.sex, stu.hobby, stu.age];
// 删
NSString *sqlStr = [NSString stringWithFormat:@"delete from stu where name = '%@'", stu.name];
// 调用
int result = sqlite3_exec(dbPoint, [sqlsStr UTF8String], nil, nil, nil);
该博客介绍了一个Objective-C方法,用于从模型中获取属性名称和类型,并将它们存储到SQLite数据库。首先,使用`objc/runtime.h`获取模型的属性列表,然后根据属性类型创建不同的查询操作(如插入、更新和删除)。在`selectALWithModel:`方法中,从数据库中检索数据并根据属性设置模型对象。

2846

被折叠的 条评论
为什么被折叠?



