0

I have found several explanations how to fix this but none of them (that I found) are simple enough for me to understand. New to Objective-c.

I am looking to access an array in multiple classes in objective c. I am trying to create a global variable (I know inefficient) in a "Globals.h" class, Initialize them in another class, and access that information in yet another class.

Heres what I have.

In Globals.h

#import <UIKit/UIKit.h>

extern NSArray *globalRows;

@interface Globals : NSObject

@end

ViewController.m

#import "Globals.h"

-(void)initGlobal{
    globalRows = [[NSArray alloc] initWithArray:rows];
}

The compiler does not want to Initialize the variable and I do not understand why.

EDIT:
The Globals class is only to hold the global variables there is no executable code in this class.

The reason I initialize in the ViewController is that is where the information needed is parsed and stored.

There is no error and the code will simply not compile.

EDIT2:

It seems I have found a very good explanation here!

Community
  • 1
  • 1
user3009729
  • 75
  • 2
  • 9
  • Why would you initialise it in another class? At lease call a method on the `Globals` class... – Wain Jan 07 '14 at 16:04
  • Is the code in initGlobal ever executed? Do you have an error? Please add more data so your question can have an answer. – Fábio Oliveira Jan 07 '14 at 16:05
  • *"There is no error and the code will simply not compile."* ?? - If the code does not compile then there should be a compiler error message. – Martin R Jan 07 '14 at 16:22

1 Answers1

1

NSObjects have a + (void)initialize method you can setup that get called during startup. You could also just call it from main as well.

Still, a better design pattern is to make a singleton to hold some global thing you want to access/use. Similarly putting the things inside your app delegate make them effectively global.

0xFADE
  • 832
  • 5
  • 7