Re: subclass says "unrecognized selector"
Re: subclass says "unrecognized selector"
- Subject: Re: subclass says "unrecognized selector"
- From: Mikkel Eide Eriksen <email@hidden>
- Date: Sat, 12 Feb 2011 19:18:32 +0100
Thanks for the suggestions, all.
I think this will probably suit my needs for now.
Mikkel
On 12/02/2011, at 17.06, Andy Lee wrote:
> "super" does not change what class the object thinks it is an instance of. (It's different from C++ in this way, if I remember my C++.) If an object is an instance of class X, [self class] *always* returns X.
>
> Suppose you call [obj tagDict] where obj is an instance of SubClass. This is what happens:
>
> "if ([self class] ..." fails, so you fall through to the [super tagDict] call. The message dispatcher sees the "super" and looks for an implementation of tagDict one level up in the class hierarchy, and finds that SuperClass implements it, so it calls that implementation.
>
> "if ([self class] ..." again fails (because [self class] still returns SubClass), so again you fall through to the [super tagDict] call. The message dispatcher looks for an implementation of tagDict one level up in the class hierarchy, but SuperClass's superclass does not implement tagDict, hence the error.
>
> I *think* this is what's happening. To test my theory, use the debugger or NSLog statements to trace the logic that is being executed.
>
> You can fix this by doing something like this (note this is quick untested code and there might be a smarter and/or more efficient approach):
>
> -(NSDictionary *)tagDictForClass:(Class)aClass
> {
> NSMutableDictionary *dict = //built from plist, different for each class
>
> if (aClass == [SuperClass class]]) {
> return dict; // we don't go above our own root
> }
>
> NSDictionary *superDict = [self tagDictForClass:[aClass superclass]];
>
> [dict addEntriesFromDictionary:superDict];
>
> return dict;
> }
>
> -(NSDictionary *)tagDict
> {
> return [self tagDictForClass:[self class]];
> }
>
> --Andy
>
>
> On Feb 12, 2011, at 8:52 AM, Mikkel Eide Eriksen wrote:
>
>> Hi all,
>>
>> 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:
>>
>> -(NSDictionary *)tagDict
>> {
>> NSMutableDictionary *dict = //built from plist, different for each class
>> if ([self class] == [SuperClass class]) {
>> return dict; // we don't go above our own root
>> }
>>
>> NSEnumerator *keyE = [[super tagDict] keyEnumerator]; //HERE
>> id key;
>>
>> while (key = [keyE nextObject]) {
>> [dict setValue:[[super tagDict] valueForKey:key]];
>> }
>>
>> return [dict copy];
>> }
>>
>> When this runs, it works fine if I call tagDict on an instance of SuperClass, but if I try with SubClass, I get:
>>
>> -[SubClass tagDict]: unrecognized selector sent to instance 0x20038efe0
>>
>> But shouldn't it keep calling upwards until I reach the SuperClass, then stop and return my finished dict? I see that it appears to be calling [SubClass tagDict] which is the very method it's already in (so the selector can't very well be unrecognized, or what?)...
>>
>> Regards,
>> Mikkel_______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> 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:
>>
>> This email sent to email@hidden
>
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden