最近在一個(gè)iOS項(xiàng)目中,利用到了Obj-c語言的運(yùn)行時(shí)反射特性,來減少一些代碼編寫的工作量,特記錄下來。
移動(dòng)互聯(lián)網(wǎng)下iOS客戶端的開發(fā),一般都會(huì)與服務(wù)端進(jìn)行通訊,也會(huì)使用到Sqlite數(shù)據(jù)庫來保存一些數(shù)據(jù),按常規(guī)的搞法,一般都需要手動(dòng)建表結(jié)構(gòu),寫實(shí)體類對(duì)象,然后寫插入、更新、查詢等語句來實(shí)現(xiàn)功能,因此想到是否有一種通用的辦法來進(jìn)行一些代碼方面的減負(fù)工作。通過這個(gè)項(xiàng)目的實(shí)踐,已經(jīng)找到了答案。
在使用這些代碼前,我們進(jìn)行了以下的約定:
1. sqlite的數(shù)據(jù)庫表名直接使用實(shí)體類的類名;
2. sqlite的數(shù)據(jù)字段使用實(shí)體類的屬性名稱;
3. sqlite的數(shù)據(jù)類型統(tǒng)一設(shè)為text(引起sqlite這種文本數(shù)據(jù)庫是動(dòng)態(tài)類型的,存儲(chǔ)的本質(zhì)都是文本)
4. 實(shí)體類的類型統(tǒng)一都設(shè)為NSString
做以上的約定,只是減少使用過程中,由于類型的不同造成不必要的麻煩,如果要支持各種類型,需要編寫各種判斷代碼,進(jìn)行格式的處理,有興趣的同學(xué)可以進(jìn)一步研究,呵呵。
下面介紹幾個(gè)重要的方法。
因?yàn)橛玫搅薕bj-c運(yùn)行時(shí)的相關(guān)方法,所以需要導(dǎo)入運(yùn)行時(shí)的頭文件。
#import
直接擴(kuò)展了NSObject,名稱叫NSObject+Property,文章末尾提供文件下載。
@interface NSObject (Property)
@end
1. 利用反射取得NSObject的屬性,并存入到數(shù)組中
- (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;
}
2. 根據(jù)屬性生成創(chuàng)建Sqlite表的語句
- (NSString *)tableSql:(NSString *)tablename{
NSMutableString *sql = [[NSMutableString alloc] init];
NSArray *array = [self getPropertyList];
[sql appendFormat:@"create table %@ (",tablename] ;
NSInteger i = 0;
for (NSString *key in array) {
if (i>0) {
[sql appendString:@","];
}
[sql appendFormat:@"%@ text",key];
i++;
}
[sql appendString:@")"];
return sql;
}
3. 把一個(gè)實(shí)體對(duì)象,封裝成字典Dictionary
- (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];
}
[dict setObject:value forKey:key];
}
return dict;
}
4. 從一個(gè)字典中還原成一個(gè)實(shí)體對(duì)象
- (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];
}
}
}
5. 返回一個(gè)對(duì)象的類型名稱
- (NSString *)className{ return [NSString stringWithUTF8String:object_getClassName(self)]; }
以上是對(duì)NSObject的一個(gè)擴(kuò)展,使用了Obj-C的Category特性
以下是與數(shù)據(jù)存儲(chǔ)相關(guān),定義為DbHelper,使用FMDB庫對(duì)sqlite進(jìn)行操作
1. 把id類型的數(shù)據(jù)對(duì)象插入到數(shù)據(jù)庫
-(void)insertObject:(id)object{
NSString *tablename = [object className];
NSMutableString *sql = [[NSMutableString alloc] init];
NSArray *array = [object getPropertyList];
[sql appendFormat:@"insert into %@ (",tablename] ;
NSInteger i = 0;
for (NSString *key in array) {
if (i>0) {
[sql appendString:@","];
}
[sql appendFormat:@"%@",key];
i++;
}
[sql appendString:@") values ("];
NSMutableArray *arrayValue = [NSMutableArray array];
i=0;
for (NSString *key in array) {
SEL selector = NSSelectorFromString(key);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id value = [object performSelector:selector];
#pragma clang diagnostic pop
if (value==nil) {
value = @"";
}
[arrayValue addObject:value];
if (i>0) {
[sql appendString:@","];
}
[sql appendString:@"?"];
i++;
}
[sql appendString:@")"];
[_db executeUpdate:sql withArgumentsInArray:arrayValue];
}
2. 把字典NSDictionary對(duì)象插入到數(shù)據(jù)庫中
在與服務(wù)器進(jìn)行交互時(shí)候,我們一般采用Json進(jìn)行數(shù)據(jù)通訊,從服務(wù)端獲取Json字符,通過JSONKit框架,反序列化成NSDictionary對(duì)象,然后插入到數(shù)據(jù)庫
生成插入的sql語句
-(NSString *)createInsertSqlByDictionary:(NSDictionary *)dict tablename:(NSString *)table{
NSMutableString *sql = [[NSMutableString alloc] init];
[sql appendFormat:@"insert into %@ (",table] ;
NSInteger i = 0;
for (NSString *key in dict.allKeys) {
if (i>0) {
[sql appendString:@","];
}
[sql appendFormat:@"%@",key];
i++;
}
[sql appendString:@") values ("];
i = 0;
for (NSString *key in dict.allKeys) {
if (i>0) {
[sql appendString:@","];
}
[sql appendFormat:@":%@",key];
i++;
}
[sql appendString:@")"];
return sql;
}
把字典插入到數(shù)據(jù)庫中
-(void)insertBySql:(NSString *)sql dict:(NSDictionary *)dict{
if (sql && sql.length>0) {
[_dbQueue inDatabase:^(FMDatabase *db) {
[db executeUpdate:sql withParameterDictionary:dict];
}];
}
}
取數(shù)據(jù)
3. 從數(shù)據(jù)庫取數(shù)據(jù),封裝成成字典,然后放入到數(shù)組中
-(NSArray *)queryDbToDictionaryArray:(NSString *)tablename sql:(NSString *)sql{
FMResultSet *resultSet=[_db executeQuery:sql];
NSArray *columnArray = [self fMSetColumnArray:resultSet];
NSMutableArray *syncArray = [[NSMutableArray alloc] init];
NSString *columnName = nil;
while ([resultSet next])
{
NSMutableDictionary *syncData = [[NSMutableDictionary alloc] init];
for(int i =0;i<columnArray.count;i++)
{
columnName = [columnArray objectAtIndex:i];
NSString *columnValue = [resultSet stringForColumn: columnName];
if (columnValue==nil) {
columnValue=@"";
}
[syncData setObject:columnValue forKey:columnName];
}
[syncArray addObject:syncData];
}
if ([syncArray count]==0) {
return nil;
}
return syncArray;
}
4. 從數(shù)據(jù)庫中取數(shù)據(jù),封裝成對(duì)象,然后放入數(shù)組中
-(NSArray *)queryDbToObjectArray:(Class )clazz sql:(NSString *)sql{
FMResultSet *resultSet=[_db executeQuery:sql];
NSArray *columnArray = [self fMSetColumnArray:resultSet];
NSMutableArray *syncArray = [[NSMutableArray alloc] init];
NSString *columnName = nil;
while ([resultSet next])
{
NSObject *obj = [[clazz alloc] init];
if (obj==nil) {
continue;
}
for(int i =0;i<columnArray.count;i++)
{
columnName = [columnArray objectAtIndex:i];
NSString *columnValue = [resultSet stringForColumn: columnName];
SEL selector = NSSelectorFromString(columnName);
if ([obj respondsToSelector:selector]) {
[obj setValue:columnValue forKeyPath:columnName ];
}
}
[syncArray addObject:obj];
}
if ([syncArray count]==0) {
return nil;
}
return syncArray;
}
在數(shù)據(jù)量很大時(shí)候,考慮到性能問題,此方法需要酌情使用。
好了,最后提供這四個(gè)文件DbHelper.h、DbHelper.m、NSObject+Property.h、NSObject+Property.m的下載。
有了這幾個(gè)東西,進(jìn)行開發(fā)就省去了很多時(shí)間和代碼量,直接動(dòng)態(tài)生成表,從服務(wù)端接口取到數(shù)據(jù),直接插入到數(shù)據(jù)庫中保存,顯示數(shù)據(jù)時(shí),從數(shù)據(jù)庫中取出數(shù)據(jù)放入到對(duì)象數(shù)組中。
由于項(xiàng)目的保密性,不可能提供整個(gè)文件的下載,只提供了關(guān)鍵的幾個(gè)文件,同學(xué)們可以根據(jù)需要進(jìn)行擴(kuò)展和研究,也歡迎一起探討,甚至拍磚。
文件下載:http://pan.baidu.com/share/link?shareid=202843&uk=85241834