Using global variables in Objective-C

烂漫一生 提交于 2019-11-28 04:01:33
trojanfoe

One way to implement global variables, and to manage their lifetime (i.e. that they are initialized) and even to provide global methods is to implement a class exposing those variables/methods and to use the singleton pattern:

GlobalVars.h:

#import <Foundation/Foundation.h>

@interface GlobalVars : NSObject
{
    NSMutableArray *_truckBoxes;
    NSMutableArray *_farmerlist;
    NSString *_farmerCardNumber;
    NSString *_fName;
}

+ (GlobalVars *)sharedInstance;

@property(strong, nonatomic, readwrite) NSMutableArray *truckBoxes;
@property(strong, nonatomic, readwrite) NSMutableArray *farmerList;
@property(strong, nonatomic, readwrite) NSString *farmerCardNumber;
@property(strong, nonatomic, readwrite) NSString *fName;

@end

GlobalVars.m:

#import "GlobalVars.h"

@implementation GlobalVars

@synthesize truckBoxes = _truckBoxes;
@synthesize farmerList = _farmerList;
@synthesize frameCardNumber = _frameCardNumber;
@synthesize fName = _fName;

+ (GlobalVars *)sharedInstance {
    static dispatch_once_t onceToken;
    static GlobalVars *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[GlobalVars alloc] init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        _truckBoxes = [[NSMutableArray alloc] init];
        _farmerlist = [[NSMutableArray alloc] init];
        // Note these aren't allocated as [[NSString alloc] init] doesn't provide a useful object
        _farmerCardNumber = nil;
        _fName = nil;
    }
    return self;
}

@end

You can then use these global variables like this, for example:

GlobalVars *globals = [GlobalVars sharedInstance];
globals.fName = @"HelloWorld.txt";
[globals.farmerList addObject:@"Old Macdonald"];
[self processList:[globals farmerList]];

However, please consider:

  • You don't need to use global variables like this; you should be able to create a model object which is created as necessary and reference to it passed to the views. This is MVC.
  • You also posted a stack trace of an unrelated issue which is extremely common with Objective-C; only you can fix this error, once you realize what it is.

The error has nothing to do with global variables. The error message

-[__NSArrayM length]: unrecognized selector sent to instance 0x8b7fbc0

indicates that you somewhere in your code assigned a NSMutableArray to farmerCardNumber instead of a NSString.

For example, if you do something like

farmerCardNumber = [someDictionary objectForKey:@"someKey"];

and [someDictionary objectForKey:@"someKey"] happens to be an array and not a string, then farmerCardNumber points to that array, even if it was declared as pointer to a string.

I am using in all my project and working fine.

GlobalResource.h

#import <Foundation/Foundation.h>

@class GlobalResource;

extern GlobalResource * R;

@interface GlobalResource : NSObject
{
    NSString *myVar;
}
+ (void) loadGlobalResources;

@property (strong, nonatomic, readwrite) NSString *myVar;

@end

GlobalResource.m

#import "GlobalResource.h"

GlobalResource *R;

@implementation GlobalResource

@synthesize myVar;

- (void) loadGlobalResources
{
    R = [[GlobalResource alloc] init];
}

- (id)init
{
    self = [super init];
    if (self) {
        myVar = @"My Value";
    }

    return self;
}

@end

How to use

#import "GlobalResource.h"

//then access value as bellow

[GlobalResource loadGlobalResource];
NSLog(@"%@",R.myVar); //value will print here

Why donot you try something like:

#import "GlobalVars.h"

NSArray *farmerlist;
NSArray *truckBoxes;
NSString *farmerCardNumber = nil;
NSString *fName = nil;
@implementation GlobalVars
{

}
@end

or

#import "GlobalVars.h"

NSArray *farmerlist;
NSArray *truckBoxes;
NSString *farmerCardNumber = @"";
NSString *fName = @"";
@implementation GlobalVars
{

}
@end

AND it seems like you have passed mutable array to your string varibale, that is why you are getting that error.

Remove the line

#import "GlobalVars.h"

from GlobalVars.m.

You can't put extern variables to *.h file.

So in GlobalVariables.m you have:

extern NSArray *farmerlist;
NSArray *farmerlist;

And:

@interface GlobalVars : NSObject
{
}

@end

@implementation GlobalVars
{

}
@end

are not needed

[edit]

for example:

Other.m

#import "GlobalVars1.h"

GlobalVars1.h

extern NSArray *truckBoxes;
extern NSArray *farmerlist;
extern NSString *farmerCardNumber;

extern NSString *fName;

GlobalVars.h

#import <UIKit/UIKit.h>

@interface GlobalVars : NSObject
{
}

@end

GlobalVars.m

#import "GlobalVars.h"

NSArray *farmerlist;
NSArray *truckBoxes;
NSString *farmerCardNumber;
NSString *fName;
@implementation GlobalVars
{

}
@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!