如何在Objective-C中使用全局变量?

时间:2021-08-06 05:38:41

How should I declare a global variable in my Objective-C project?

我应该如何在Objective-C项目中声明一个全局变量?

4 个解决方案

#1


24  

Traditionally, global variables are declared in a header, and defined in a source file. Other source files only need to know how it is declared to use it (i.e. its type and its name). As long as the variable is defined somewhere in a source file, the linker will be able to find it and appropriately link all the references in other source files to the definition.

传统上,全局变量在标头中声明,并在源文件中定义。其他源文件只需要知道如何声明它的使用方式(即它的类型和名称)。只要变量在源文件中的某处定义,链接器就能够找到它并将其他源文件中的所有引用适当地链接到定义。

Somewhere in your header, you would declare a global variable like this:

在标题的某处,您将声明一个如下全局变量:

extern int GlobalInt;

The extern part tells the compiler that this is just a declaration that an object of type int identified by GlobalInt exists. It may be defined later or it may not (it is not the compiler's responsibility to ensure it exists, that is the linker's job). It is similar to a function prototype in this regard.

extern部分告诉编译器这只是一个声明,即GlobalInt标识的int类型的对象存在。它可以在以后定义,也可以不定义(编译器不负责确保它存在,这是链接器的工作)。在这方面,它类似于功能原型。

In one of your source files, you define the GlobalInt integer:

在您的一个源文件中,您定义了GlobalInt整数:

int GlobalInt = 4;

Now, each file that includes the header will have access to GlobalInt, because the header says it exists, so the compiler is happy, and the linker will see it in one of your source files, so it too will be happy. Just don't define it twice!

现在,包含标题的每个文件都可以访问GlobalInt,因为标题表明它存在,所以编译器很高兴,链接器会在你的一个源文件中看到它,所以它也会很高兴。只是不要定义两次!

However


You should consider whether or not this approach is useful. Global variables get messy for a number of reasons (trying to find out exactly where it is defined or declared, threading issues), there is usually not a need for global variables. You should perhaps consider using a singleton approach.

您应该考虑这种方法是否有用。全局变量由于多种原因而变得混乱(试图找出确切定义或声明的位置,线程问题),通常不需要全局变量。您应该考虑使用单例方法。

#2


11  

Don't. Global variables are often a sign of poor design. A common replacement in Objective-C is a class method that returns an object (that may or may not be a singleton), such as [NSUserDefaults standardUserDefaults] or [UIDevice currentDevice].

别。全局变量通常是设计不佳的标志。 Objective-C中的常见替换是返回对象(可能是也可能不是单例)的类方法,例如[NSUserDefaults standardUserDefaults]或[UIDevice currentDevice]。

However, if you must use a global variable, read on.

但是,如果必须使用全局变量,请继续阅读。

In your header:

在你的标题中:

extern NSString *someString;
extern NSInteger someInteger;

In your implementation file:

在您的实现文件中:

NSString *someString = @"DEFAULT_VALUE";
NSInteger someInteger = DEFAULT_VALUE;

#3


2  

In my experience there are few instances when a program doesn't need, at least, some sort of data or utility/helper methods that can be accessed throughout the program.

根据我的经验,当程序不需要至少某种可以在整个程序中访问的某种数据或实用程序/帮助程序方法时,很少有实例。

They way I deal with this, rather than using global variables is to create what I call a 'project applicance', which is essentially just a class with a bunch of static methods.

他们处理这个问题,而不是使用全局变量来创建我称之为“项目应用程序”的东西,它本质上只是一个带有一堆静态方法的类。

It could be implemented multiple ways, but I use a singleton and just have the static methods call through to the single instance of the appliance class. For example, in my project Oovium I have:

它可以通过多种方式实现,但我使用单例并且只需要将静态方法调用到设备类的单个实例。例如,在我的项目Oovium中我有:

Oovium.h:

Oovium.h:

@interface Oovium : NSObject {
    UIWindow* _window;
}

+ (UIWindow*) window;

Oovium.m:

Oovium.m:

@implementation Oovium

static Oovium* oovium;

- (UIWindow*) window {return _window;}

+ (void) initialize {
    oovium = [[Oovium alloc] init];
}

+ (UIWindow*) window {return [oovium window];}

I then include Oovium.h in my Oovium_Prefix.pch file so that it is automatically included in all of my files.

然后我将Oovium.h包含在我的Oovium_Prefix.pch文件中,以便它自动包含在我的所有文件中。

#4


-2  

Globals rock! I don't know what everyone is scared of. I used them successfully here.

全球摇滚!我不知道每个人都害怕什么。我在这里成功使用了它们。

Passing Data between View Controllers

在视图控制器之间传递数据

Also used UIStepper to adjust values in another viewController. I could see them being an issue is larger programs, and in my opinion the singleton thing is just a masked global. Keep it simple, if your app is simple that is.

还使用UIStepper调整另一个viewController中的值。我可以看到他们是一个问题是更大的程序,在我看来,单身的事情只是一个蒙面的全球。如果您的应用程序很简单,请保持简单。

#1


24  

Traditionally, global variables are declared in a header, and defined in a source file. Other source files only need to know how it is declared to use it (i.e. its type and its name). As long as the variable is defined somewhere in a source file, the linker will be able to find it and appropriately link all the references in other source files to the definition.

传统上,全局变量在标头中声明,并在源文件中定义。其他源文件只需要知道如何声明它的使用方式(即它的类型和名称)。只要变量在源文件中的某处定义,链接器就能够找到它并将其他源文件中的所有引用适当地链接到定义。

Somewhere in your header, you would declare a global variable like this:

在标题的某处,您将声明一个如下全局变量:

extern int GlobalInt;

The extern part tells the compiler that this is just a declaration that an object of type int identified by GlobalInt exists. It may be defined later or it may not (it is not the compiler's responsibility to ensure it exists, that is the linker's job). It is similar to a function prototype in this regard.

extern部分告诉编译器这只是一个声明,即GlobalInt标识的int类型的对象存在。它可以在以后定义,也可以不定义(编译器不负责确保它存在,这是链接器的工作)。在这方面,它类似于功能原型。

In one of your source files, you define the GlobalInt integer:

在您的一个源文件中,您定义了GlobalInt整数:

int GlobalInt = 4;

Now, each file that includes the header will have access to GlobalInt, because the header says it exists, so the compiler is happy, and the linker will see it in one of your source files, so it too will be happy. Just don't define it twice!

现在,包含标题的每个文件都可以访问GlobalInt,因为标题表明它存在,所以编译器很高兴,链接器会在你的一个源文件中看到它,所以它也会很高兴。只是不要定义两次!

However


You should consider whether or not this approach is useful. Global variables get messy for a number of reasons (trying to find out exactly where it is defined or declared, threading issues), there is usually not a need for global variables. You should perhaps consider using a singleton approach.

您应该考虑这种方法是否有用。全局变量由于多种原因而变得混乱(试图找出确切定义或声明的位置,线程问题),通常不需要全局变量。您应该考虑使用单例方法。

#2


11  

Don't. Global variables are often a sign of poor design. A common replacement in Objective-C is a class method that returns an object (that may or may not be a singleton), such as [NSUserDefaults standardUserDefaults] or [UIDevice currentDevice].

别。全局变量通常是设计不佳的标志。 Objective-C中的常见替换是返回对象(可能是也可能不是单例)的类方法,例如[NSUserDefaults standardUserDefaults]或[UIDevice currentDevice]。

However, if you must use a global variable, read on.

但是,如果必须使用全局变量,请继续阅读。

In your header:

在你的标题中:

extern NSString *someString;
extern NSInteger someInteger;

In your implementation file:

在您的实现文件中:

NSString *someString = @"DEFAULT_VALUE";
NSInteger someInteger = DEFAULT_VALUE;

#3


2  

In my experience there are few instances when a program doesn't need, at least, some sort of data or utility/helper methods that can be accessed throughout the program.

根据我的经验,当程序不需要至少某种可以在整个程序中访问的某种数据或实用程序/帮助程序方法时,很少有实例。

They way I deal with this, rather than using global variables is to create what I call a 'project applicance', which is essentially just a class with a bunch of static methods.

他们处理这个问题,而不是使用全局变量来创建我称之为“项目应用程序”的东西,它本质上只是一个带有一堆静态方法的类。

It could be implemented multiple ways, but I use a singleton and just have the static methods call through to the single instance of the appliance class. For example, in my project Oovium I have:

它可以通过多种方式实现,但我使用单例并且只需要将静态方法调用到设备类的单个实例。例如,在我的项目Oovium中我有:

Oovium.h:

Oovium.h:

@interface Oovium : NSObject {
    UIWindow* _window;
}

+ (UIWindow*) window;

Oovium.m:

Oovium.m:

@implementation Oovium

static Oovium* oovium;

- (UIWindow*) window {return _window;}

+ (void) initialize {
    oovium = [[Oovium alloc] init];
}

+ (UIWindow*) window {return [oovium window];}

I then include Oovium.h in my Oovium_Prefix.pch file so that it is automatically included in all of my files.

然后我将Oovium.h包含在我的Oovium_Prefix.pch文件中,以便它自动包含在我的所有文件中。

#4


-2  

Globals rock! I don't know what everyone is scared of. I used them successfully here.

全球摇滚!我不知道每个人都害怕什么。我在这里成功使用了它们。

Passing Data between View Controllers

在视图控制器之间传递数据

Also used UIStepper to adjust values in another viewController. I could see them being an issue is larger programs, and in my opinion the singleton thing is just a masked global. Keep it simple, if your app is simple that is.

还使用UIStepper调整另一个viewController中的值。我可以看到他们是一个问题是更大的程序,在我看来,单身的事情只是一个蒙面的全球。如果您的应用程序很简单,请保持简单。