My iphone code with a global variable does not work, please inform

advertisements

I'm new to objective-c and I searched and read several posts here on how to create "global variable" but I just can't get it to work right, so far I can create it and check it but the values are not persisting on another views, my global var is an array of a custom object called "profile", I would like to be able to read and write that array from any view of my iphone app (tabbarapplication delegate);

Helper.h

@interface Helper : NSObject {
    int globalInteger;
    NSMutableArray *profiles;
}

@property (nonatomic, retain) NSMutableArray *profiles;

// message from which our instance is obtained
+ (Helper *)sharedInstance;

Helper.m

#import "Helper.h"

@implementation Helper

@synthesize profiles, globalInteger;

+ (Helper *)sharedInstance
{
    // the instance of this class is stored here
    static Helper *myInstance = nil;

    // check to see if an instance already exists
    if (nil == myInstance) {
        myInstance  = [[[self class] alloc] init];
        // initialize variables here
    }
    // return the instance of this class
    return myInstance;
}

ACertainViewController.m

//Initialize Policies Array
NSMutableArray *profs = [[Helper instance] profiles];
profs = [[NSMutableArray alloc] init];

//Sample Data
Profile *prof1 = [[Profile alloc] init];
prof1.name = @"John";

//add
[profs addObject:prof1];
[[[Helper instance] profiles] addObject:prof1];


After this point if I check the global var "profiles" contents again it returns count == 0; As of the globalInteger var I don't even know how to set its value to be able to read somewhere else in the app. Any help is much appreciated! Thanks!!!


Declare your NSMutableArray in your AppDelegate (i.e. MyAppDelegate) class. Then from another class (like your view controller), you can do this:

#import "MyAppDelegate.h"

MyAppDelegate *aDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
aDelegate.profiles = .... // or do whatever you need to do with the profiles property.

hope that helps.