Problem in accessing value of variable of a parent class

Discussion in 'iPhone/iPad Programming' started by iphonejudy, Feb 26, 2009.

  1. macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    Hi,


    I need to access a value of variable of A class in B class.

    class A
    {

    NSString *name;

    }

    @property (nonatomic, retain) NSString * name;

    then i do some calculations in the name.Now name ="hello123"

    class B

    {

    A *aobject=[[Address alloc]init];
    NSLog(@"In B class: %@", aobject.name);
    }

    I got the below message
    In B class =null.

    how can i get the value "hello123" from A class
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    That's because aobject is a brand new instance of the first class and as such has absolutely no knowledge of the initialization you did in the other instance. You need to pass the value another way. You could use a datasource style pattern or pass the original class as a property.
     
  3. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #3
    you mean

    to access name as A.name?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Class A is not a parent (or super) class of Class B unless you have B defined as:
    Code:
    @interface B : A
    If you don't, they are just separate classes that happen to need to share data.
     
  5. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #5

    Shal i know the exact syntax for that?
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I don't understand your question.
     
  7. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #7
    I need to access the content of a variable from one class to another class.

    In document they give the below syntax for sharing variable between classes.

    classA aobject=[[classA alloc] init];

    [[aobject sharedInstance] variable1];

    But if i use the above code,i got null value in variable1.
    My problem is ,it doesnot access the value of variable.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Is there a method in ClassA that sets the new value of variable1? Are you calling that method somewhere between when you init your instance of ClassA (which is, presumably, where variable1 gets set to nil) and you access the value of variable1?
     
  9. macrumors newbie

    Joined:
    Feb 23, 2009
    #9
    A couple things you need to do.

    As your code stands, class B is not a subclass of A as dejo has pointed out. However, with the code you have it doesn't really matter if class B is a subclass of class A or not.

    I assume you expect the call to NSLog to output the following:

    In B class: hello123

    Has class A got an initialise method (i.e. an init method)? To get the NSLog output above you probably want to add an init method to class A (i.e. override the init method of class A's superclass). Something like the following will give you the output you require:

    - (id) init {
    self = [super init];

    if (self) {
    //I'm sure there are better ways to do this
    name = [[NSString alloc] initWithString:mad:"hello123"];
    ..
    }
    ...
    return self;
    }

    Couple of other points...

    I'm sure someone else can tell us how primitive types and objects of a class are initialised in Objective-C if they are not explicitly initialised in the init method of a class.

    Generally you should define your classes in the following way (if they are not a subclass of some other class):

    class A : NSObject {
    ...

    This way, they inherit from the root class NSObject. The way you have coded it, class A is defined as another root class.

    Hope the above helps.
     

Share This Page