How can we access the variables declared in another class without using extern ?

Discussion in 'iOS Programming' started by srikanthrongali, May 13, 2010.

  1. srikanthrongali macrumors newbie

    Joined:
    Apr 30, 2010
    #1
    I have a ClassA, ClassB, ClassC.
    In ClassA,

    Code:
    @interface ClassA : NSObject 
    {
    	NSString *enemyDescription;
    	int numberOfEnemies;
    }
    
    @property(nonatomic, retain) NSString *enemyDescription;
    @property(nonatomic, assign) int numberOfEnemies;
    
    I want to use these values in classB.
    I used
    C
    Code:
    lassA * aclassA = [[ClassA alloc]init]; in classB. 
    So, I can get the values from ClassA and use them in ClassB.
    But, I read that
    Code:
    ClassA * aclassA = [[ClassA alloc]init]; 
    creates an empty object. So, we cannot access any values from ClassB in to ClassA.

    But, in some cases I am able to access the values and I can see the values by using NSLog in ClassB.
    Code:
    NSLog("string: %@", aclassA. enemyDescription); 
    But, some times I see the values are 0.
    What is the correct method of accessing the values from ClassA into ClassB ?
    Thank you.
     
  2. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #3
    Can you use properties or are you looking for something like a C++ friend?
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    What do you mean by access?

    You can always send classA setter and getter messages to access anything (not only class variables, and not only variables), if you've created the appropriate setters and getters in classA.

    Other, less clean, ways include stuffing pointers to the various variables into all classes that need the access, peeking/poking directly into class structures, and etc.
     

Share This Page