Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: John McCall <email@hidden>
- Date: Mon, 11 Mar 2013 14:45:11 -0700
On Mar 11, 2013, at 2:02 PM, Dave <email@hidden> wrote:
>
> On 11 Mar 2013, at 20:53, John McCall wrote:
>
>> On Mar 11, 2013, at 1:33 PM, Dave <email@hidden> wrote:
>>> On 11 Mar 2013, at 20:26, Mike Abdullah wrote:
>>>>>
>>>>> I had assumed (and I thought I'd done something like this before) that the:
>>>>>
>>>>> myDict = [[super class] newDict];
>>>>>
>>>>> statement would call newDict in BaseClass???? Instead it calls the version in NewClass and goes into an infinite loop!!
>>>>
>>>> Yes. [super class] calls super's implementation of the -class method. You haven't overridden -class, so it does the same thing as [self class].
>>>>
>>>> People often make the same mistake in trying to do [super respondsToSelector…
>>>>
>>>> I'm guessing what you're really after is [[self superclass] newDict]
>>>>
>>>
>>> Thanks a Million, yes that's what I wanted!
>>
>> Are you sure? This will indeed call the superclass's 'newDict', but the 'self' object will be the superclass, not your class. That means it'll (almost certainly) create an instance of your class's superclass. The easier way to do this is just [Foo newDict], where Foo is the name of your superclass.
>>
>> If you want to invoke your superclass method, but with your class as 'self' — i.e. if you want to create an instance of your class — you should use [super newDict].
>
> I don't know what you mean by:
>
>> i.e. if you want to create an instance of your class
>
> I don't want to create an instance of my class, I want the class to return a dictionary, that gets things added from the current class down through the inheritance hierarchy,
>
> BaseClass alloc's a dict and puts in some data,
> Subclass1 calls base class and it's it own data.
> Subclass2 calls subclass1.
>
> How is this creating an instance of any object except the NSDict?
Where do you think Subclass1's "own data" goes if the object allocated is actually a BaseClass?
> I could just say [BaseClass newDict], but the reason I didn't is so the code is not dependent on knowing the base class, e.g. it can move position in the hierarchy and still call the correct class method.
Do you want the created dictionary to have the instance variables and methods you've defined in your subclass or not? If so, you need to create an instance of your subclass.
I think you are having basic conceptual problems with inheritance here.
Also, please change your mail client to use your first and last names in messages you send.
John.
_______________________________________________
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