Can i have a single NSMutableArray in my multiple views application?
Asked Answered
D

4

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.

Devlin answered 3/10, 2011 at 14:0 Comment(3)
"I have to add and remove object to that array." What do you mean by "that array"? Which array? What do you mean by "appDelegate's array"?Godliman
sorry, my bad. I have edited the question.Devlin
As suggested by alinoz, you should think about using a singleton.Godliman
R
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.

Ruler answered 3/10, 2011 at 14:8 Comment(13)
in DataClass.m, Do we have to release arrGlobal or not?Devlin
No you don't have to release it. It is itself released when application ends.Ruler
DataClass *obj=[DataClass getInstance]; [obj.arrGlobal addObject:self.currentRunData]; NSLog(@"obj.arrGlobal count: %d", obj.arrGlobal.count); but obj.arrGlobal.count is always 0Devlin
Array count is always integer. It should be %i and not %d.Ruler
its still 0. didn't make any differenceDevlin
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.Ruler
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.Ceremonious
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?Devlin
@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.Devlin
See this.Ruler
@Devlin -- Did you ever actually create the NSMutableArray, or did you just declare its property??Ceremonious
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 thatDevlin
@Devlin -- That's sounding like you just declared the property and never actually created an NSMutableArray and assigned it to the property.Ceremonious
P
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.

Parch answered 3/10, 2011 at 14:2 Comment(0)
R
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.

Ruler answered 3/10, 2011 at 14:8 Comment(13)
in DataClass.m, Do we have to release arrGlobal or not?Devlin
No you don't have to release it. It is itself released when application ends.Ruler
DataClass *obj=[DataClass getInstance]; [obj.arrGlobal addObject:self.currentRunData]; NSLog(@"obj.arrGlobal count: %d", obj.arrGlobal.count); but obj.arrGlobal.count is always 0Devlin
Array count is always integer. It should be %i and not %d.Ruler
its still 0. didn't make any differenceDevlin
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.Ruler
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.Ceremonious
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?Devlin
@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.Devlin
See this.Ruler
@Devlin -- Did you ever actually create the NSMutableArray, or did you just declare its property??Ceremonious
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 thatDevlin
@Devlin -- That's sounding like you just declared the property and never actually created an NSMutableArray and assigned it to the property.Ceremonious
C
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.

Ceremonious answered 3/10, 2011 at 14:59 Comment(2)
It is always suggested to use singleton instead of appDelegate. Also see this discussion.Ruler
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.Ceremonious
B
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.

Badinage answered 18/6, 2013 at 14:8 Comment(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!Codie

© 2022 - 2024 — McMap. All rights reserved.