Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphonejudy

macrumors 6502
Original poster
Sep 12, 2008
301
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
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 

iphonejudy

macrumors 6502
Original poster
Sep 12, 2008
301
1
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.


Shal i know the exact syntax for that?
 

iphonejudy

macrumors 6502
Original poster
Sep 12, 2008
301
1
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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?
 

shusseina

macrumors newbie
Feb 23, 2009
14
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.