Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Dave <email@hidden>
- Date: Tue, 12 Mar 2013 07:31:17 +0000
On 11 Mar 2013, at 21:45, John McCall wrote:
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'm not allocating a base class or any other kind of class except a
dictionary. Did I say anything about instance data? No. The data I
mean is NOT instance data, how could it be? There is no object
allocated in this example (except the dictionary). I am using class +
methods and they work on the Class,
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.
There are no instance variables as I keep saying, this no instance,
so how can there be instance variables?
I think you are having basic conceptual problems with inheritance
here.
I don't think you understand the difference between class methods and
instance methods. Here is an example of what I mean:
BaseClass (Class not an instance of a class).
+(MSMutableDictionary*) newDict
{
MSMutableDictionary* myDict;
myDict = [[MSMutableDictionary alloc] init];
[myDIct setObect:@"SomeaValueX" forKey:@"someKeyX"];
return myDict;
}
Subclass1:
+(MSMutableDictionary*) newDict
{
MSMutableDictionary* myDict;
myDict = [super newDict];
[myDIct setObect:@"SomeValueY" forKey:@"someKeyY"];
return myDict;
}
Subclass2:
+(MSMutableDictionary*) newDict
{
MSMutableDictionary* myDict;
myDict = [super newDict];
[myDIct setObect:@"SomeValueZ" forKey:@"someKeyZ"];
return myDict;
}
No instance data in site and no instances of the objects in question,
just class methods.
All the Best
Dave
_______________________________________________
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