On Sat, Feb 12, 2011 at 5:52 AM, Mikkel Eide Eriksen
<mikkel.erik...@gmail.com> wrote:
> I think I may have misunderstood something about how super works. In trying 
> to build a dictionary that contains key/value pairs from the class itself as 
> well as super classes up to an arbitrary height, I've hit a wall. Simplified, 
> I have two classes, SuperClass and SubClass. In SuperClass, the following 
> method is implemented:

It doesn't look like your code doesn't require any instance
information at all. I'd probably implement it as a class method:

+ (NSDictionary *)tagDict {
  NSMutableDictionary *tags = GetTagsFromPlistForClass(self);
  if ([super respondsToSelector:@selector(tagDict)])
    [tags addEntriesFromDictionary:[super tagDict]];
  return [[tags copy] autorelease];
}

Or you can be even more robust and use a protocol, that way if an
ancestor class happens to implement a method named -tagDict that is
unrelated to your intended meaning, you don't suffer from a collision.
(If you're not writing a framework, this is probably overkill).

@protocol TagDictProtocol
+ (NSDictionary *)tagDict;
@end

@interface SuperClass : NSObject <TagDictProtocol>
@end

@implementation SuperClass
+ (NSDictionary *)tagDict {
  NSMutableDictionary *tags = GetTagsFromPlistForClass(self);
  if ([super conformsToProtocol:@protocol(TagDictProtocol)])
    [tags addEntriesFromDictionary:[super tagDict]];
  return [[tags copy] autorelease];
}
@end

--Kyle Sluder
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to