Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-28-2009, 12:20 PM   #1 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default accessing containing class variable from subclass

Hi everyone!

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?

Can someone help me?
neduhamel is offline   Reply With Quote
Old 06-28-2009, 01:08 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
Default

Quote:
Originally Posted by neduhamel View Post
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.
__________________
smasher is offline   Reply With Quote
Old 06-29-2009, 01:55 AM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

Hi Smasher !

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:

x = my_parent.member_data;
neduhamel is offline   Reply With Quote
Old 06-29-2009, 02:18 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
Default

Quote:
Originally Posted by neduhamel View Post
Hi Smasher !

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.

Code:
-(void) initWithParent: (id) parent
{
     self=[super init];
     if (self){
          my_parent=parent;
     }
     return self;
}
Note that I used "super" here because I need to call the init method of the class that B inherits from (B's superclass; probably NSObject?)
__________________
smasher is offline   Reply With Quote
Old 06-29-2009, 03:05 AM   #5 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

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:

Code:
//class_A.h
//-----------
@interface class_A 
{	
@public
    class_B my_class_B;  
}
-(void)init;
@end
Code:
//class_A.m
//-----------
@implementation class_A
	
-(void) init
{
    [my_class_B initWithParent:self]; //is this ok?
}

@end
Code:
//class_B.h
//------------
@interface class_B
{
@private
    class_A my_parent;  //This what i doubt is good...
}
-(void) initWithParent: (id) parent;
@end
Code:
//class_B.m
//------------
@implementation class_B

-(void) initWithParent: (id) parent
{
     self=[super init];
     if (self){
          my_parent=parent;
     }
     return self;
}

@end
neduhamel is offline   Reply With Quote
Old 06-29-2009, 06:31 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 61
Posts: 505
Default

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:

myOwner.someAvariable

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 06-29-2009, 12:53 PM   #7 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

Great, it works!!

Thank you very much to both of you!
neduhamel is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,292
Threads: 39,080
Posts: 171,361
Top Poster: smasher (2,575)
Welcome to our newest member, omerk
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0