Craig
Craig

Reputation: 13

Access to variables through a couple of other classes

I'm reposting this question that I had posted a day ago. When I described the problem before I left out some crucial information that caused people to go down the wrong path in trying to offer a solution. Hopefully this is better explained.

I've got a situatiion in Objective-C where I'm trying to access an object's variable through another object. The classes (simplified):

A.h

@interface A : NSObject {  
  NSMutableArray *someStuff;  
}  

@property (nonatomic, retain) NSMutableArray *someStuff;    

@end 

A.m

@implementation A  

@synthesize someStuff;  

//  blah, blah, blah  

Then, because I'm doing an iPhone app, there is an app delegate that contains a variable of this object type:

AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate> {  

   A *aPtr;  
}  

@property (nonatomic, retain) A *aPtr;  
@end  

AppDelegate.m

@implementation AppDelegate  

@synthesize aPtr; 

Then, in another class (in this case a view controller), I'm trying to access 'someStuff' in this manner:

AViewController.m

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  

NSMutableArray *someArray = appDelegate.aPtr.someStuff;  

So, the problem is that this blows up in fine fashion. I had thought that if you do the @porperty designation, and the @synthesize in the implementation, then you should be able to access the variables directly as the compiler actually creates the get / set functions.

I think I'm too much of a Java junkie to understand why this won't work. Can anyone elighten me?

Many thanks,

Craig

Upvotes: 0

Views: 100

Answers (2)

saadnib
saadnib

Reputation: 11145

I think its not working because you haven't initialize arr at anywhere, make a function in A.m and in app delegate call that function when you are initializing the class A and also in A.m in that function don't forget to initialize the array.

Upvotes: 0

Jiva DeVoe
Jiva DeVoe

Reputation: 1348

Yeah, you can do this. Follow the instructions in the comment, in other words, typecast your app delegate to avoid warnings there. Also, make sure you're importing the interface for your AppDelegate and for your A class in the file you're trying to access this from.

Upvotes: 1

Related Questions