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
2009-12-09 08:38:38
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
2009-12-09 08:44:02
can u please tell me how to do it in objective c
Muniraj
2009-12-09 08:44:42
that's C++. in C there's no special syntax for globals.
David Maymudes
2009-12-09 08:44:48
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
2009-12-09 08:51:23
@Muniraj: That looks like C++, not C. BTW: Please write "you" instead of "u", it hurts looking at it.
Georg
2009-12-09 08:51:38
+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
2009-12-09 09:52:45
A:
Single source file:
int myGlobal;
Header file:
extern int myGlobal;
Any file including the header:
myGlobal = 10;
PfhorSlayer
2009-12-09 10:41:00