A: 

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
Mark
Thank you, that was quite insightful! I will try it your way.
Arakyd
+1  A: 

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.)

kubi
Arakyd
I only had to search my Google Reader content, so I got lucky. Good luck!
kubi