0

I have a navigational based application which has multiple views. Is it possible to use one single NSMutableArray for the whole applicaiton? Can i add objects to that NSMutableArray in one view and then remove object from the same NSMutableArray from some other view? I tried

 myappAppDelegate *appDelegate = (myappAppDelegate *)[[UIApplication sharedApplication] delegate];

but it gives me null when i try to access appDelegate's array. If anyone can give me any idea or helping link or tutrorial. Thanks in advance.

Piscean
  • 3,069
  • 12
  • 47
  • 96

4 Answers4

2

For your type of issue I would use a singleton.

http://en.wikipedia.org/wiki/Singleton_pattern

The appdelegate is a singleton too but you can reduce a bit the number of coded lines if you use your own singleton.

alinoz
  • 2,822
  • 22
  • 38
2

If you are having multiple views in your application, and in that case you want to have a variable accessible to every view, you should always create a Model/Data(singleton) class and define the variable in it. Something like this :

//DataClass.h      

@interface DataClass : NSObject {    

NSMutableArray *arrGlobal;     

}    
@property(nonatomic,retain)NSMutableArray *arrGlobal;   
+(DataClass*)getInstance;    
@end  



//DataClass.m    
@implementation DataClass    
@synthesize arrGlobal;    
static DataClass *instance =nil;    
+(DataClass *)getInstance    
{    
    @synchronized(self)    
    {    
        if(instance==nil)    
        {    

            instance= [DataClass new];    
        }    
    }    
    return instance;    
}    

Now in your view controller you need to call this method as :

DataClass *obj=[DataClass getInstance];  
obj.arrGlobal = arrLocal; 

This variable will be accessible to every view controller. You just have to create an instance of Data class.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • in DataClass.m, Do we have to release arrGlobal or not? – Piscean Oct 03 '11 at 14:18
  • No you don't have to release it. It is itself released when application ends. – Nitish Oct 03 '11 at 14:21
  • DataClass *obj=[DataClass getInstance]; [obj.arrGlobal addObject:self.currentRunData]; NSLog(@"obj.arrGlobal count: %d", obj.arrGlobal.count); but obj.arrGlobal.count is always 0 – Piscean Oct 03 '11 at 14:29
  • Array count is always integer. It should be %i and not %d. – Nitish Oct 03 '11 at 14:34
  • its still 0. didn't make any difference – Piscean Oct 03 '11 at 14:38
  • I just checked out. Count will be displayed as 0 on log. Do one thing. Just debug your application and check if at this point array has objects or not. – Nitish Oct 03 '11 at 14:41
  • There are arguments for and against singleton vs using AppDelegate (though mostly the arguments on both sides are emotional). The AppDelegate approach should work, if properly implemented. – Hot Licks Oct 03 '11 at 14:46
  • nops. there is no object in the array but currentRunData object exists. addObject is not putting it in obj.arrGlobal or may be not accessing arrGlobal correctly. Do we need to initialize the arrGlobal or not? – Piscean Oct 03 '11 at 14:50
  • @DanielRHicks: its my first time and i am new to iphone develoment. May be i am not implementing AppDelegate approach correctly. I tried to find out details but everywhere they said make property of NSMUtableArray in your AppDelegate and then use that line of code. I did that but didn't work. – Piscean Oct 03 '11 at 14:52
  • See [this](http://www.iphonedevsdk.com/forum/iphone-sdk-development/48815-nsmutablearray-does-not-update-singleton-class.html). – Nitish Oct 03 '11 at 14:57
  • @Piscean -- Did you ever actually create the NSMutableArray, or did you just declare its property?? – Hot Licks Oct 03 '11 at 15:06
  • in interface myAppDelegate : NSObject{ NSMutableArray *array;} property (nonatomic, retain) NSMutableArray *array; and then in .m synthesize array; i skipped @ signs because stackoverflow didn't allow me to do that – Piscean Oct 03 '11 at 15:30
  • @Piscean -- That's sounding like you just declared the property and never actually created an NSMutableArray and assigned it to the property. – Hot Licks Oct 04 '11 at 16:02
1

On the Singleton approach add this

instance.arrGlobal = [[NSMutableArray alloc] init];

this way:

@synchronized(self)    
{    
    if(instance==nil)    
    {    

        instance= [DataClass new];
        instance.arrGlobal = [[NSMutableArray alloc] init];
    }    
}    
return instance;

This way you can initilize the array and use it properly.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Panta
  • 11
  • 1
  • This needs more upvotes. I couldn't get the above answer to work until I initialized the global array as you did here. Thanks! – Tim Aug 13 '14 at 23:26
1

The AppDelegate approach should work, and you should probably figure out why it's not working, even if you go with a singleton.

The statement to get your appDelegate pointer appears to be correct, so I'm guessing that the pointer to the array is either not getting set (and retained) in your myappDelegate class, or you did not create the AppDelegate instance correctly in the first place.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • 2
    It is always suggested to use [singleton instead of appDelegate](http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html). Also see [this](http://stackoverflow.com/questions/5155437/is-it-good-practice-to-use-appdelegate-for-data-manipulation-and-handling) discussion. – Nitish Oct 03 '11 at 15:07
  • I've seen some of the discussions by self-appointed authorities. But in any event the OP should figure out what he's doing wrong, since there's nothing special about AppDelegate and he's likely making a mistake he'll make elsewhere unless he learns the error of his ways. – Hot Licks Oct 03 '11 at 15:17