I’m new to Objective-C and I’d like to abstract my database access using a model class like this:
@interface LectureModel : NSMutableDictionary
{
}
-(NSString*)title;
-(NSDate*)begin;
...
@end
I use the dictionary methods setValue:forKey: to store attributes and return these in the getters. Now I want to read these models from a sqlite database by using the Class dynamically.
+ (NSArray*)findBySQL:(NSString*)sql intoModelClass:(Class)modelClass
{
NSMutableArray* models = [[[NSMutableArray alloc] init] autorelease];
sqlite3* db = sqlite3_open(...);
sqlite3_stmt* result = NULL;
sqlite3_prepare_v2(db, [sql UTF8String], -1, &result, NULL);
while(sqlite3_step(result) == SQLITE_ROW)
{
id modelInstance = [[modelClass alloc] init];
for (int i = 0; i < sqlite3_column_count(result); ++i)
{
NSString* key = [NSString stringWithUTF8String:sqlite3_column_name(result, i)];
NSString* value = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(result, i)];
if([modelInstance respondsToSelector:@selector(setValue:forKey:)])
[modelInstance setValue:value forKey:key];
}
[models addObject:modelInstance];
}
sqlite3_finalize(result);
sqlite3_close(db);
return models;
}
Funny thing is, the respondsToSelector: works, but if I try (in the debugger) to step over [modelInstance setValue:value forKey:key], it will throw an exception, and the stacktrace looks like:
#0 0x302ac924 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ #1 0x991d9509 in objc_exception_throw #2 0x302d6e4d in __CFRequireConcreteImplementation #3 0x00024d92 in +[DBManager findBySQL:intoModelClass:] at DBManager.m:114 #4 0x0001ea86 in -[FavoritesViewController initializeTableData:] at FavoritesViewController.m:423 #5 0x0001ee41 in -[FavoritesViewController initializeTableData] at FavoritesViewController.m:540 #6 0x305359da in __NSFireDelayedPerform #7 0x302454a0 in CFRunLoopRunSpecific #8 0x30244628 in CFRunLoopRunInMode #9 0x32044c31 in GSEventRunModal #10 0x32044cf6 in GSEventRun #11 0x309021ee in UIApplicationMain #12 0x00002988 in main at main.m:14
So, what’s wrong with this? Presumably I’m doing something really stupid and just don’t see it…
Many thanks in advance for your answers,
Arakyd :..
I think you need to start looking at Objective C from the start again it is not C++
Your code example does not tell us that modelClass is a LectureModel so there are things missing so I might have misunderstood some things.
First you have to call [super init] directly see Apple Objective C Primer.
In Objective C you use composition instead than inheritance more often than in C++ – In this example I would use delegation to allow use of dictionary. LectureModel will pass any messages it does not understand to the dictionary attribute. Also from NSDictionary
NSDictionary and NSMutableDictionary are part of a class cluster, so the objects you create with this interface are not actual instances of the these two classes. Rather, the instances belong to one of their private subclasses.
So NSMutableDictionary is not really a good class to extend.
This is partial code there are many parts missing including dealloc