views:

318

answers:

3

can anyone tell how to declare and change global variables in objective c

+3  A: 

Just the same way that you would in C. Are you having any particular problem?

Phil Nash
in c if u want to change global variable u use ::variable to change the value of it. But such feature is not available in objective c
Muniraj
can u please tell me how to do it in objective c
Muniraj
that's C++. in C there's no special syntax for globals.
David Maymudes
In fact, even in C++, you only *need* to use the global scope resolution operator (::) to disambiguate from identifiers with the same name in the same (or closer) namespace.
Phil Nash
@Muniraj: That looks like C++, not C. BTW: Please write "you" instead of "u", it hurts looking at it.
Georg
+2  A: 

On a related note; global variables are (very) generally speaking considered a Bad Thing™. In Obj-C the more common approach is making them a property on a singleton object, ensuring at least some encapsulation is taking place.

In an AppKit/UIKit application; a global variable might more properly be a property on your application delegate; another, somewhat more involved, option is making a singleton class for encapsulating the variable and related methods.

Williham Totland
A: 

Single source file:

int myGlobal;

Header file:

extern int myGlobal;

Any file including the header:

myGlobal = 10;
PfhorSlayer