Question

I have implemented Opencv in one of my project. In an opencv .mm file, I need to access the appdelegate object for global access of an array but it is giving Linker Error all the time (When I am trying to import the AppDelegate Class). So I have created an NSObject Class, still I can not access the Global Array which is created as the @property of the NSObject (I have imported the NSObject class in the .mm file). Array is giving the NULL value. So where do I create the Array by which I can access the array in my whole project?

I can not use the NSUSerDefaults and DocumentDirectory. Because I want to save a lots of images and video links in that array, So I can not use NSUserDefaults and I don't want to use again the phone memory for the temp file as because I am picking the images from the Photolibrary, so DocumentDirectory is discarded, So Is there any way by which I can create the global array and access them from the openCV files i.e objective CPP files?

Was it helpful?

Solution

you can make class as UserdataSingleton which overrides NSObject. which you can use all over your application to share data globally (for your case array). this code template may help you:

#import <Foundation/Foundation.h>

@interface UserDataSingleton : NSObject
{
    @private
    NSArray *globalArray;
}
+(UserDataSingleton *) getInstance;  
-(void)saveInUserDatasingletonWithArray:(NSArray *)array;  
-(NSDictionary *)getGlobalArray;

@end

and implementation file will be some thing like:

#import "UserDataSingleton.h"  

@implementation UserDataSingleton  

static UserDataSingleton *userDataSingletonInstance;  


+(UserDataSingleton *) getInstance  
{ 

    if (userDataSingletonInstance == nil)   {
     userDataSingletonInstance = [[UserDataSingleton alloc] init]; 
}  
    return userDataSingletonInstance; 
}

-(void)saveInUserDatasingletonWithArray:(NSArray *)array 
{
    globalArray = array; 
}
-(NSDictionary *)getGlobalDictionary 
{ 
    return globalArray; 
} 
@end 

================== usage:

#import "UserDataSingleton.h"
#define USERDATASINGLETON (UserDataSingleton *)[UserDataSingleton getInstance]

......................your code...

 NSArray *this_IS_Array_Populated_here_For_Global_Access = [NSArray alloc] initWith....];

[USERDATASINGLETON saveInUserDatasingletonWithArray:this_IS_Array_Populated_here_For_Global_Access];//you put your array for global access. 

later some where in any other view or view controller you can get that global array for example lets say you have YourViewController class:

NSMutableArray *yourArrayFromWebResponse = [USERDATASINGLETON getGlobalArray];

thanks

OTHER TIPS

Note that Objective C is a superset of C. Thus, you can have regular C variables in your program. My favorite is to make a global variable for my appDelegate. Put this in your app delegate's .h file:

MyAppDelegateClass * appDelegate;

(change "MyAppDelegateClass" to your appDelegate's class name) and put this in application: didFinishLaunchingWithOption: method in your app delegate's .m file:

    appDelegate = self;     // set universal global variable

Now just #import your app delegate's .h file and have access to your appDelegate from anywhere.

You may try to create a singleton objet like and access it from everywhere

you could define a macro in your delegate like so:

#define sharedAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

and then in the class which you want to access your array import your AppDelegate class, boiler plate would be:

#import AppDelegate.h

and then just use:

NSArray *arrayYouWanted = [AppDelegate sharedAppDelegate].yourPropertyArrayName;

You can also store it in NSUserDefaults if you want to persist it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top