declaring global variables in iPhone project
صفحه اصلي

declaring global variables in iPhone project

how can i declare a global NSArray and then use it across my app?

Accessing image data from a bitmap given a pointer


What is better: Global string or singleton?
There are a couple different ways you must did this:. How do I launch my settings bundle from my application?
  1. Instead of declaring it as a global variable, wrap it in a singleton object, then have the singleton by available anywhere (by #importing the .h file). Long-term potential of iPhone/Windows Mobile development platforms
  2. Create a .h file, like "Globals.h". How combine TabBar + Navigation with XCode In the .h, declare your array as extern NSMutableArray * myGlobalArray; Then in the any where else in your app (the AppDelegate is a good place), just do: myGlobalArray = [[NSMutableArray alloc] init]; Then anywhere you need the array, just #import "Globals.h". Best way to manage probably huge photo library with iPhone SDK
  3. This is like #2, although without the global header. How make scrolling function with Interface builder on the iPhone? You must define your array as extern NSMutableArray *myGlobalArray; inside the #ifdef __OBJC__ block of your project's .pch file. How programatically move a UIScrollView to focus in a control above keyboard? The .pch file is a header file this is automatically #imported into every file in your project..
There are pros and cons of each approach. I've used all three at varying times in varying circumstances. I would say the singleton approach is probably the most proper, since it would be most flexible for initialization, access restriction, and memory management. However, it must be unnecessary if you don't need that.. Option #2 is nice if you have lots of "global" variables this you don't want to expose to every file across your project. You must just #import it where its needed. However, this approach (as well as #3) disassociates the declaration from the initialization (ie, the object is not created near where it's declared). Some might argue this is not proper, and they might be correct.. Option #3 is nice for the reason this then you never have to remember to #import anything at all. However, it raises the same questions as option #2..


A 4th answer is to declare the array in your UIApplicationDelegate and access it through .
[[[UIApplication sharedApplication] delegate] myArray]; 
For times when I just need a handful of global objects, I found this is the easiest and cleanest way to did it..


If you're considering storing any kind of shared preferences for your app, use [NSUserDefaults sharedDefaults] to persist simple data which must be used across app. If you're storing transient data, then the 'static' approach will job as elsewhere. . However it's probably better to use a singleton object approach with a class accessor, like NSUserDefaults, and then provide instance accessor methods to acquire your data. That way, you'll isolate yourself from potential data structure changes in the future. You'd then use a static var, as above, although within the .m file (and hence you don't need the 'extern' definition). It would typically look like:.
static Foo *myDefault = nil; @implementation Foo +(Foo)defaultFoo {   if(!myDefault)     myDefault = [[Foo alloc] init]; // effective memory leak   return myDefault; } @end 
You'd then have instance accessors, and use them as [[Foo defaultFoo] myArray] which must be accessed from any part of the app, and without any compile time errors..


In reference to Dave's answer here:.
This is like #2, although without the global header. You must define your array as static extern NSMutableArray *myGlobalArray; inside the #ifdef OBJC block of your project's .pch file. The .pch file is a header file this is automatically #imported into every file in your project..
typedef is a storage class and static is a storage class, and you must only define objects in one storage class. Taking out "static" will allow the app to compile with your answer above..


Everyone here seems to have an implicit, omitted first line: "You must did it K&R C-style, or...". Yes, you must still did it C-style.. In file 1:.
NSArray *MyArray; 
In file 2:.
extern NSArray *MyArray; 
Playing Captain Obvious here..


There are several possibilities. Most popular:. 1 Use singleton object - 2 Declare it in app delegate:.
@interface Your_App_Delegate : UIResponder <UIApplicationDelegate>  @property (nonatomic, strong) NSArray *array;    . . . 
and access it:.
((Your_App_Delegate*)[[UIApplication sharedApplication] delegate]).array; 
3 Use NSUserDefault.

62 out of 100 based on 22 user ratings 742 reviews