通过方法提取出勒的所有属性,赋值废话不多说,直接看代码:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface FMBaseData : NSObject
/*
*得到属性列表
*/
- (NSArray *)getPropertyList;
- (NSArray *)getPropertyList: (Class)clazz;
/*
*创建数据库的sql语句
*/
- (NSString *)tableSql:(NSString *)tablename;
/*
*根据类名字创建数据库
*/
- (NSString *)tableSql;
/*
*把类型转为字典
*/
- (NSDictionary *)convertDictionary;
/*
*用字典初始化
*/
- (id)initWithDictionary:(NSDictionary *)dict;
/*
*类名字
*/
- (NSString *)className;
@end
.m文件
#import "FMBaseData.h"
@implementation FMBaseData
- (id)initWithDictionary:(NSDictionary *)dict
{
self = [self init];
if(self)
{
[self dictionaryForObject:dict];
}
return self;
}
- (NSArray *)getPropertyList
{
return [self getPropertyList:[self class]];
}
- (NSArray *)getPropertyList: (Class)clazz
{
u_int count;
objc_property_t *properties = class_copyPropertyList(clazz, &count);
NSMutableArray *propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* propertyName = property_getName(properties[i]);
[propertyArray addObject: [NSString stringWithUTF8String: propertyName]];
}
free(properties);
return propertyArray;
}
- (NSString *)tableSql:(NSString *)tablename
{
NSMutableString *sql = [[NSMutableString alloc] init];
NSArray *array = [self getPropertyList];
[sql appendFormat:@"create table IF NOT EXISTS %@ (",tablename];
NSInteger i = 0;
for (NSString *key in array) {
if (i>0)
{
[sql appendString:@","];
}
[sql appendFormat:@"%@ text",key];
i++;
}
[sql appendString:@")"];
print(sql);
return sql;
}
- (NSString *)tableSql
{
return [self tableSql:[self className]];
}
- (NSDictionary *)convertDictionary
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSArray *propertyList = [self getPropertyList];
for (NSString *key in propertyList)
{
SEL selector = NSSelectorFromString(key);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id value = [self performSelector:selector];
#pragma clang diagnostic pop
if (value == nil)
{
value = [NSNull null];
//空不赋值
continue;
}
[dict setObject:value forKey:key];
}
return dict;
}
- (NSString *)className
{
return [NSString stringWithUTF8String:object_getClassName(self)];
}
- (BOOL)checkPropertyName:(NSString *)name
{
unsigned int propCount, i;
objc_property_t* properties = class_copyPropertyList([self class], &propCount);
for (i = 0; i < propCount; i++) {
objc_property_t prop = properties[i];
const char *propName = property_getName(prop);
if(propName) {
NSString *_name = [NSString stringWithCString:propName encoding:NSUTF8StringEncoding];
if ([name isEqualToString:_name]) {
return YES;
}
}
}
return NO;
}
- (void)dictionaryForObject:(NSDictionary*) dict
{
for (NSString *key in [dict allKeys])
{
id value = [dict objectForKey:key];
if (value==[NSNull null])
{
continue;
}
if ([value isKindOfClass:[NSDictionary class]])
{
id subObj = [self valueForKey:key];
if (subObj)
[subObj dictionaryForObject:value];
}
else
{
[self setValue:value forKeyPath:key];
}
}
}
dictionaryForObject
方法可以根据自己的需要改写