全局 int 变量目标 c

发布于 2024-10-18 17:45:11 字数 56 浏览 3 评论 0原文

我想在一个类中声明一个 static int 变量,并可以在其他每个类中访问它。最好的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神爱温柔 2024-10-25 17:45:11

Objective C 中没有静态类变量。您可以在类的实现文件中将其创建为文件范围静态变量,并在类中提供静态 setter 和 getter 方法。

或者,您可以将其设为老式全局变量,并在 .h 文件中使用 extern 声明。前一种方法更灵活——您可以在setter方法中添加额外的检查,例如,后者更少打字,并且避免了方法调用开销。

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.

著墨染雨君画夕 2024-10-25 17:45:11

您可以尝试以下一些方法

  • 在appdelegate中声明全局变量

  • 创建单例类并放置全局变量
    那里。


使用appdelegate

appdelegate也是一种单例类

函数定义:

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

函数调用:

[self ReadAppDelegateInstanceVariable:@"someInstanceVariableName"];

使用自己的单例类

类只能存在一个实例。

单例声明示例:

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

单例实现示例:

方法 1: 使用 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

方法 2: 不使用 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

函数定义:

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

函数调用:

[self ReadSingleTonInstanceVariable:@"SomeInstanceVariableName"];

NSString 到 int:

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

据我所知,以一种方式执行此操作不存在性能问题。

我总是更喜欢单例类而不是appdelegate,因为代码不会混乱,而且我认为过度使用appdelegate作为臭代码

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.

恋竹姑娘 2024-10-25 17:45:11

这打破了一些模式,我不会使用它。

无论如何,如果您在应用程序委托中声明一个属性,那么您可以调用:
[[NSApp delegate] myVar] 任何地方。

您究竟打算如何使用这个变量?

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?

梦途 2024-10-25 17:45:11

如何使用单例类来保存任何类都可以访问的所有变量,并且范围是应用程序的整个运行时。
查看此链接

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文