Re: Confusing problems with inheritance in Objective-C
Re: Confusing problems with inheritance in Objective-C
- Subject: Re: Confusing problems with inheritance in Objective-C
- From: David Reed <email@hidden>
- Date: Mon, 21 Jun 2004 22:50:30 -0400
Darren,
Your trouble stems from the fact that you're trying to create a
subclass of the public abstract class representing a class cluster (see
http://developer.apple.com/documentation/Cocoa/Conceptual/Foundation/
Concepts/ClassClusters.html).
The topic of subclassing abstract classes has been discussed before;
see the list archives or Google. However, it looks to me like a
category on NSMutableDictionary would do what you want without
subclassing. In cases where you simply want to add functionality
(methods, but not instance variables), a category can often suffice.
Categories are described here:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
3objc_language_overview/chapter_3_section_7.html#//apple_ref/doc/uid/
20001424/TPXREF138
Hope this helps,
David.
On Monday, Jun 21, 2004, at 22:31 US/Eastern, Darren Ford wrote:
Hey all,
I've been having some problems trying to implement inheritance using
Objective-C (more specifically, attempting to create a class that
inherits from NSMutableDictionary).
What I'm finding is that when one of my methods (for example, the
'setDate' function below), that when it attempts to call the super's
method, I get the error :-
-setObject:forKey: only defined for abstract class. Define
-[MyInheritedDictionaryClass setObject:forKey:]'
Isn't NSMutableDictionary a concrete class (ie. I can instantiate
one using alloc/init?)
I've been programming in C++ for many years now, and I think the
problem is probably that I haven't fully understood the Obj-C concept
of inheritance and I'm trying to use my C++ knowledge to define my
class. Can anyone shed any light on why I'm seeing this problem?
I've attached my class definitions to the end of the email.
Cheers and thanks -- Darren.
---------- header
@interface MyInheritedDictionaryClass : NSMutableDictionary
- (NSDate *)theDate;
- (void)setTheDate:(NSDate *)newTheDate;
- (NSMutableArray *)theFiles;
- (void)setTheFiles:(NSMutableArray *)newTheFiles;
@end
---------- source
@implementation MyInheritedDictionaryClass
- (id) init
{
return [super init];
}
-(void) dealloc
{
[super dealloc];
}
- (NSDate *)theDate
{
return [super objectForKey:@"date"];
}
- (void)setTheDate:(NSDate *)newTheDate
{
[super setObject:newTheDate forKey:@"date"];
}
- (NSMutableArray *)theFiles
{
return [super objectForKey:@"files"];
}
- (void)setTheFiles:(NSMutableArray *)newTheFiles
{
[super setObject:newTheFiles forKey:@"files"];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.