开发者

My iphone code with a global variable is not working, please advise

开发者 https://www.devze.com 2023-04-12 07:46 出处:网络
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开发者_StackOverflow中文版 work right, so far I can create it and che

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开发者_StackOverflow中文版 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!!!


You need to move "static Helper *myInstance = nil" outside the class method. Now, you're setting it to nil each time and so each time you access the sharedInstance it gets reallocated.


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.


You need to alloc/initialize the profiles array. try this:

// the instance of this class is stored here: thanks @onnoweb for pointing this out
    static Helper *myInstance = nil;
    + (Helper *)sharedInstance
    {
        // check to see if an instance already exists
        if (nil == myInstance) {
            myInstance  = [[[self class] alloc] init];
            // initialize variables here
            profiles=[[NSMutableArray alloc] init];
        }
        // return the instance of this class
        return myInstance;
    }

Also take a look here: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html


"instance variable 'profiles' accessed in class method"

Where the code you posted has the comment // initialize variables here, are you actually accessing the variable profiles? Instead use myInstance.profiles.

"warning: incomplete implementation of class 'Helper' warning: method definition for '+instance' not found"

There some code you're not showing us, or the code you posted is different from your real code. There is no method declared or defined in the code you posted called 'instance', but you are attempting to call a method called 'instance'. There is one by a different name called 'sharedInstance'. Most likely in your real code you mixed up the names and declared 'instance' but defined 'sharedInstance'. Pick one name and stick with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消