views:

39

answers:

2

How can I declare a global integer and use it across my app?

+2  A: 
//SomeFile.h
extern int n;

//SomeFile.m
int n;

//SomeOtherFile.m
#import "SomeFile.h"

Now you can use global n in your SomeOtherFile.m file

Vladimir
I have just tried this and it threw 12 errors concerning my variable. I tried CODE:://otherviewcontroller.hextern int lives;//otherviewcotroller.mlives = 5;and I also use it here in several times.//levelcleared.m#import "otherviewcontroller.h"score = lives * 1000; ::END OF CODE
Adam_D
you didn't initialize correctly. use int lives = 5; in otherviewcontroller.m
Jesse Naugher
tried that mate, it just throws a warning followed by the other 11 errors. Anybody else know what I'm doing wrong?
Adam_D
What warnings and errors do you get?
Vladimir
A: 

You can store it as a property in your application delegate and access your application delegate everywhere using this singleton :

[[UIApplication sharedApplication] delegate]
Pierre Valade