开发者

Global int variable objective c

开发者 https://www.devze.com 2023-02-12 21:39 出处:网络
I want to declare a static int variable in one clas开发者_如何学编程s and have access to it in every other class. What is the best way to do this?There are no static class variables in Objective C. Yo

I want to declare a static int variable in one clas开发者_如何学编程s and have access to it in every other class. What is the best way to do this?


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.


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.


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?


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

精彩评论

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