I know this is gonna sound "n00b", but i recently moved to obj-c, so i'm not very familiar with it yet.
Here's my question :
I have a class A with a member variable which is a class B.
So the class B does NOT inherit from class A, but it is a subclass (is this the right term?) of the class A.
Now, in one of the methods of class B, i need to access a member variable of class A which is public, and i can't figure out how to do it!!
Please do not give me an answer such as "why don't you change the architecture" because this is just a simple example, and my real architecture is way more complicated, but the problem summarize as above.
I tried to use "superclass" and "super" but i think this is only for inherited classes, right?
So the class B does NOT inherit from class A, but it is a subclass (is this the right term?) of the class A.
If class B does not inherit from class A, then "subclass" is probably not the right word. Subclass would imply, to me, that B inherits from A. Let's say they have a parent-child relationship instead.
If class B only needs to know its parent in a certain method, you could pass the parent into that method. Otherwise, if B needs to contact its parent a lot, you could give class B an instance variable called "parent" , and write an initWithParent: method that sets the parent variable. If the parent changes a lot, you should probably make b.parent a property instead.
Thanks a lot for your reply.
the "initWithParent" sound like a good idea, but my problem is that i don't know technicaly how to define the parent class as a member of the child class.
i would guess something like this :
-(void) initWithParent
{
my_parent = super;
}
is this the right way? And then i can access is using:
Thanks a lot for your reply.
the "initWithParent" sound like a good idea, but my problem is that i don't know technically how to define the parent class as a member of the child class.
How did you define the child class as a member of the parent class? You need the same thing - an instance variable and possibly a property.
This is how you'd define the initWithParent method. It will look funny if you've never written an init method before.
i'm sorry, i feel like if i do as you say, it's gonna create a endless loop as the child will have a member which is the parent which has a child which has a parent....and so on. Am i wrong?
Here's what i understand from your advice:
From the interface files for class_A and class_B, it seems that B is indeed not a subclass of A. They are just two classes that need to know about each other. So make sure each class has a #import directive for the other class's interface file. When you say that class A contains an instance of class B, that has nothing to do with subclassing or inheritance. If that is the case, then go ahead and write initWithParent as suggested by smasher. Although "initWithOwner" might be a more appropriate name if class A and class B do not have any subclass relationship. In your class B interface file, define an instance variable that is a pointer to an instance of class A, such as:
Class_A *myOwner;
In your initWithOwner method, set myOwner equal to the parameter that is passed by Class A. Since you are holding on to this pointer for some length of time, you should probable define it as a retained ivar and use the setter method to set it, as in:
self.myOwner = parameterPassed;
and then remember to release myOwner in your Class B dealloc method. Now whenever you want to access an instance variable of Class A from within a Class B method, just use: