40media 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?




Populate a UITableView with data

1:



Dynamically change url or WordPress theme if UserAgent is iPhone
There are a couple different ways you can do this:.
inheritance in objective C
  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).
    Program to change the phone numbers a call is redirected to if a mobile phone cannot be reached?
  2. Create a .h file, like "Globals.h".


    How to change UITabBar Selection color
    In the .h, declare your array as extern NSMutableArray * myGlobalArray; Then in the somewhere 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".
    iPhone SDK: GameKit and large files + connection lost
  3. This is like #2, but without the global header.


    remove every type of subview from uiscrollview?
    You can define your array as extern NSMutableArray *myGlobalArray; inside the #ifdef __OBJC__ block of your project's .pch file.


    Read header files and do something before full photo upload happens
    The .pch file is a header file that 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 can be unnecessary if you don't need that.. Option #2 is nice if you have lots of "global" variables that you don't want to expose to every file across your project.

You can 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 because then you never have to remember to #import anything at all.

However, it raises the same questions as option #2..


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 do it..


3:


If you're considering storing some kind of shared preferences for your app, use [NSUserDefaults sharedDefaults] to persist simple data which can be used across app.

If you're storing transient data, then the 'static' approach will work 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, but 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 can be accessed from any part of the app, and without any compile time errors..


4:


In reference to Dave's answer here:.
This is like #2, but without the global header.

You can 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 that is automatically #imported into every file in your project..
typedef is a storage class and static is a storage class, and you can only define objects in one storage class.

Taking out "static" will allow the app to compile with your answer above..


5:


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


6:


There are several possibilities.

Most popular:. 1 Use singleton object - http://www.galloway.me.uk/tutorials/singleton-classes/. 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.



82 out of 100 based on 72 user ratings 322 reviews