First of all, I tried almost all the solutions given in stackoverflow but I didn\'t succeed in implement global vars, I even did a step by step tutorial and still I get the
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
Remove the line
#import "GlobalVars.h"
from GlobalVars.m.
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:
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.
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.
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