views:

673

answers:

2

I'm trying to pass an NSArray containing strings for use in another class than the one in which it is generated. Therefore I have made it a property as following:

loginController.h

#import <Cocoa/Cocoa.h>  

@interface loginController : NSObject {  
NSArray *myArray;  
}  

@property (nonatomic, retain) NSArray * myArray;  

@end

loginController.m

#import "loginController.h"  

@implementation loginController

@synthesize myArray;  

@end

The question is how the interface and implementation files of the class in which I want to access myArray should look like. At this moment the interface file looks as following:

#import <Cocoa/Cocoa.h>  
#import "loginController.h"  

@interface viewController : NSObject {  
NSArray* myArray;  
}  

@property (nonatomic,retain) NSArray* myArray;  

@end

I'm not sure if this is correct and how to implementation should look if I want to use myArray in it. Can somebody help?

+2  A: 

I think you might have a fundamental misunderstanding as to how properties work, or what they're for. They're simply generated getters and setters. They don't make objects automagically visible from any class in the program.

See Apple's docs for more info.

Nick Veys
+1  A: 

If your LoginController had a reference to your ViewController object, you could do something that is along these lines in your LoginController class:

#import "loginController.h"  

@implementation loginController

@synthesize myArray;
@synthesize myViewController; // this property needs to be
                              // declared in loginController.h

// This method represents some hypothetical method that
// would be invoked at some point after both 'myViewController'
// and 'myArray' have been instantiated and initialized
// (for example it could be 'awakeFromNib').
-(void)someMethod
{
    myViewController.myArray = myArray;
}

@end

However, I want to caution you that it might not be a good architectural decision to have your LoginController and your ViewController know about each other. I only post this code as an example of how to actually pass the array between two objects; in most designs, I would think that both LoginController and ViewController are subordinate to some containing object (such as an application-level controller, for example), which would be the object setting myArray on the two controller instances.

erikprice