开发者

ObjC: Alloc instance of Class and performing selectors on it leads to __CFRequireConcreteImplementation

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 mod开发者_StackOverflowels;
}

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

@interface LectureModel : NSObject
{
  NSMutableDictionary *dict;
}

...

@end



@implementation LectureModel


-(LectureModel*)init
{
  if (self = [super init]) {
    dict = [[NSMutableDictionary dictionary]retain]; // need to keep this
  }
  return self;
}

- (void)forwardInvocation:(NSInvocation *)invocation   // See NSObject for details
{
    SEL aSelector = [invocation selector];

    [invocation invokeWithTarget:dict];   // let dict do the work
}

...
@end


You're trying to subclass NSMutableDictionary? There's a blog entry for that!

http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html

First bit of advice?

Best practice: don't subclass

He's right. Trying to subclass a class not designed to be subclassed in Cocoa is usually harder than you think it'll be, and 99% of the time isn't necessary anyway.


Looking at the code you posted, it doesn't look like you need to subclass anyway. Create a class that implements the title and begins methods and use a dictionary to handle the underlying storage.

edit2: Clarified my last statement about subclassing. Some classes in Cocoa are designed to be subclassed (UIViewController, NSObject) and do so willingly, others are not designed to be subclassed and require a lot of careful coding (and in some cases, inside knowledge of how the objects work) to get them to work correctly. Bottom line: unless you're subclassing one of the classes designed for it, be careful and consider other alternatives (decorator class, categories, delegation, notification, etc.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜