0

I have an App Delegate and a 3 view controllers in my project. I have a variable(a NSMutable Array) in my App Delegate which I want to access from my view controllers. So I decided to create a pointer to my App Delegate and access the variables. Here is my code:

iSolveMathAppDelegate.h

#import <UIKit/UIKit.h>

@interface iSolveMathAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UITabBarController *tabBarController;
    NSMutableArray *tmpArray;
    }


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *tmpArray; // variable I want to access
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

iSolveMathAppDelegate.m

#import "iSolveMathAppDelegate.h"


@implementation iSolveMathAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize tmpArray;
...
- (void)dealloc {
    [tabBarController release];
    [window release];
    [tmpArray release];
    [super dealloc];
}


@end

The view controller class from which I want to access the tmpArray.

referenceViewController.h

#import <UIKit/UIKit.h>

@class iSolveMathAppDelegate;

@interface referenceViewController : UITableViewController {
    NSMutableArray *equationTypes;
    iSolveMathAppDelegate *data;

}

@property(nonatomic, retain) NSMutableArray *equationTypes;
@property(nonatomic, retain) iSolveMathAppDelegate *data;

@end

And finally referenceViewController.m

#import "referenceViewController.h"


    @implementation referenceViewController
    @synthesize equationTypes, data;

     data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 
//says that initializer element is not constant...ERROR!




        - (void)viewDidLoad {
            [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"equationTemplates"ofType:@"plist"];
        data.tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
        self.equationTypes = data.tmpArray;
  [data.tmpArray release]; // obviously none of these work, as data is not set.

    }


    - (void)dealloc {
        [super dealloc];
        [equationTypes release];
        [data release];
    }


    @end

So anyway at the line data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; the compiler says that the initializer element is not constant.

I have scrouged the web for answers, and for all it seems to work...but no dice for me :( Can you please advice me on where I have gone wrong? I am using XCode 3.2 and iOS SDK 3....maybe the SDK is the problem.

Thank You

Aravind
  • 284
  • 1
  • 4
  • 10

2 Answers2

2

That line of code isn't in a method or function, so the compiler is treating it as the definition of a compile-time constant or static/global variable. Those need constant values for initialization.

You should put the assignment of data within a method. A good place would be -viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    ...
}
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • Hi, thanks for the prompt reply. That certainly worked. However when I try to now acces tmpArray by going data.tmpArray, I get an error code daying that I am requesting for tmpArray in something not a structure or union. Could you help me with that as well? Thank you once again. – Aravind Mar 23 '11 at 01:02
  • Import the app delegate's header. If you don't, then the compiler won't know what properties it declares. – Jonathan Grynspan Mar 23 '11 at 12:20
0

I figured out the struct and union problem. All I had to do was change @class iSolveAppDelegate to #import "iSolveAppDelegate.h" in my referenceViewController.h file. Thanks Jonathan for your help!

Aravind
  • 284
  • 1
  • 4
  • 10