Hi,
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 :..