Using global variables in Objective-C

后端 未结 6 845
一个人的身影
一个人的身影 2020-12-07 19:33

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

相关标签:
6条回答
  • 2020-12-07 20:02

    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
    
    0 讨论(0)
  • 2020-12-07 20:05

    Remove the line

    #import "GlobalVars.h"
    

    from GlobalVars.m.

    0 讨论(0)
  • 2020-12-07 20:06

    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.
    0 讨论(0)
  • 2020-12-07 20:06

    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.

    0 讨论(0)
  • 2020-12-07 20:10

    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.

    0 讨论(0)
  • 2020-12-07 20:11

    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
    
    0 讨论(0)
提交回复
热议问题