Reputation: 13
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):
@interface A : NSObject {
NSMutableArray *someStuff;
}
@property (nonatomic, retain) NSMutableArray *someStuff;
@end
@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:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
A *aPtr;
}
@property (nonatomic, retain) A *aPtr;
@end
@implementation AppDelegate
@synthesize aPtr;
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
aPtr = [[A alloc] init]; // blah, blah, blah
Then, in another class (in this case a view controller), I'm trying to access 'someStuff' in this manner:
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
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
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