Global int variable objective c

后端 未结 4 1138
误落风尘
误落风尘 2020-12-01 22:31

I want to declare a static int variable in one class and have access to it in every other class. What is the best way to do this?

相关标签:
4条回答
  • 2020-12-01 23:00

    That breaks some patterns, I'd not use it.

    Anyway, if you declare a property in your app delegate then you can call:
    [[NSApp delegate] myVar] anywhere.

    How exactly do you intent to use this variable?

    0 讨论(0)
  • 2020-12-01 23:03

    Here are some ways you could try

    • Declaring the global variables in appdelegate

    • Creating a singleton class and putting the global variables there.


    Using appdelegate

    appdelegate is also a kind of singleton class

    Function definition:

    -(NSString*)ReadAppDelegateInstanceVariable:(NSString*)InstVar 
    {
    AppDelegate *appDel=(AppDelegate *)[UIApplication sharedApplication].delegate;
    return [appDel valueForKey:InstVar];
    }
    

    Function Calling:

    [self ReadAppDelegateInstanceVariable:@"someInstanceVariableName"];
    

    Using your own singleton class

    Only one instance of class can exist.

    Sample singleton declaration:

    @interface SigletonClass : NSObject
    {
      //declare instance variable
    }
    + (id)sharedSingletonClass;
    @end
    

    Sample singleton implementation:

    Approach 1: Using GCD

    @implementation SigletonClass
    
    + (id)sharedSingletonClass {
    
        static SigletonClass *sharedClass = nil;
    
        static dispatch_once_t onceToken;//The way we ensure that it’s only created once is by using the dispatch_once method from Grand Central Dispatch (GCD).
    
       dispatch_once(&onceToken, ^{
         sharedClass = [[self alloc] init];
      });
    
        return sharedClass;
    }
    
    - (id)init {
        if (self = [super init]) {
         //init instance variable        
        }
        return self;
    }
    @end
    

    Approach 2: Without using GCD

    @implementation SigletonClass
    
    + (id)sharedSingletonClass {
    
        static SigletonClass *sharedClass = nil;
    
        @synchronized(self) {//To safeguard threading issues
    
            if (sharedClass == nil)
                sharedClass = [[self alloc] init];
        }
    
        return sharedClass;
    }
    
    - (id)init {
        if (self = [super init]) {
         //init instance variable        
        }
        return self;
    }
    @end
    

    Function definition:

    -(NSString*)ReadSingleTonInstanceVariable:(NSString*)InstVar 
    {
       SigletonClass sObj=[SigletonClass sharedSingletonClass];
       return [sObj valueForKey:InstVar];
    }
    

    Function Calling:

    [self ReadSingleTonInstanceVariable:@"SomeInstanceVariableName"];
    

    NSString to int:

    -(int)ConvertToIntFromString:(NSString*)str
    {
        return str.intValue;
    }
    

    As far as I’m aware, there are no performance issues with doing it one way over another.

    I always prefer singleton class rather than appdelegate because the code will be clutter free and I consider overusing appdelegate as smelly code.

    0 讨论(0)
  • 2020-12-01 23:16

    There are no static class variables in Objective C. You can create it as a file-scope static variable in the class' implementation file and provide static setter and getter methods in the class.

    Or you can make it an old-school global, with an extern declaration in the .h file. The former approach is more flexible - you can add extra checks in the setter method, for example, the latter is less typing, and avoids the method call overhead.

    0 讨论(0)
  • 2020-12-01 23:18

    How about using a singleton class to save all the variables which any class can access and scope is the entire runtime of the app. Check out this link

    0 讨论(0)
提交回复
热议问题