13

How do I declare a variable in the main.m file so that it is available in all the classes?

If I simply declare it in the main function, the compiler says it's undeclared in the class method.

Must I declare it in an object like this?

@public
type variable;
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
thepepp
  • 304
  • 1
  • 3
  • 15

3 Answers3

25

All you need is to use plain old C global variables.

First, define a variable in your main.m, before your main function:

#import <...>

// Your global variable definition.
type variable;

int main() {
    ...

Second, you need to let other source files know about it. You need to declare it in some .h file and import that file in all .m files you need your variable in:

// .h file

// Declaration of your variable.    
extern type variable;

Note that you cannot assign a value to variable in declaration block, otherwise it becomes a definition of that variable, and you end with linker error complaining on multiple definitions of the same name.

To make things clear: each variable can be declared multiple times (Declaration says that this variable exists somewhere), but defined only once (definition actually creates memory for that variable).

But beware, global variables are a bad coding practice, because their value may be unexpectedly changed in any of files, so you may encounter hard to debug errors. You can avoid global variables using Singleton pattern, for example.

iHunter
  • 6,205
  • 3
  • 38
  • 56
  • Imo, global variables actually make a lot of sense in Objective C, given the tendency for a lot of things to naturally be singletons, especially view controllers. In this case they act more like constants than variables. – devios1 Aug 04 '13 at 04:39
  • Actually singletons use a global variable - the shared instance -, thus making every member global; you're only replacing one kind of globalness with another one. – Cristik Oct 22 '15 at 08:43
  • Globals variables (or more precisely global constants) do make a lot of sense when you have a constant value or string that you want to use in multiple objects. Make sure you use the "const" keyword properly in your global variable declarations and definitions to ensure that the global variable cannot be changed. e.g. `NSString * const kSomeConstantString = @""; // constant pointer`. See https://stackoverflow.com/questions/6828831/sending-const-nsstring-to-parameter-of-type-nsstring-discards-qualifier/6830983#6830983 – RobK Sep 14 '22 at 16:53
10

Not really sure why you want to do it, but you could if you wanted.

main.m:

int someGlobal = 0; ///< Added outside any function, at the top say.

SomeClass.m:

extern int someGlobal; ///< Added at the top, outside the class implementation.

...

- (void)useGlobal {
    NSLog(@"someGlobal = %i", someGlobal);
    someGlobal = 5;
    NSLog(@"someGlobal = %i", someGlobal);
}

But please, think carefully before embarking on using something like this!

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • Why be careful? I don't understand what the risk would be – Allison Mar 25 '13 at 18:37
  • 1
    Risk would be unmaintainable code or possibly hard to trace bugs. – mattjgalloway Mar 25 '13 at 20:03
  • Every single post I see in the Internet about global vars has warnings about "being careful", and some other people saying "why be careful?". Haha, it is always the same Jazz. People that don't want to be careful: think that there must be a hidden reason why other people is warning you over and over again ;) – tothemario Aug 18 '14 at 18:55
2

Besides debugging, I see no reason to even try and modify the main.m file to directly interact with your application logic.

You can try to define a constant on Your_project_name_Prefix.pch file, if that suits your needs. Or declare a static variable on your application delegate, or any of the classes of your app.

To learn more about constants and static variables, follow this link:

http://iosdevelopertips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rigo Vides
  • 1,364
  • 13
  • 17
  • But this doesn't work, if you need readwrite access to the var. E. g. if you want to store a password or a session id accessible to all classes. – K. Biermann May 27 '14 at 11:29