objective-c 在 iPhone 项目中声明全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1249131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:02:57  来源:igfitidea点击:

declaring global variables in iPhone project

objective-ciphoneiphone-sdk-3.0

提问by user140736

how can i declare a global NSArray and then use it across my app?

我如何声明一个全局 NSArray 然后在我的应用程序中使用它?

回答by Dave DeLong

There are a couple different ways you can do this:

有几种不同的方法可以做到这一点:

  1. Instead of declaring it as a global variable, wrap it in a singleton object, then have the singleton by available anywhere (by #importing the .h file)

  2. Create a .h file, like "Globals.h". In the .h, declare your array as extern NSMutableArray * myGlobalArray;Then in the somewhere else in your app (the AppDelegate is a good place), just do: myGlobalArray = [[NSMutableArray alloc] init];Then anywhere you need the array, just #import "Globals.h"

  3. This is like #2, but without the global header. You can define your array as extern NSMutableArray *myGlobalArray;inside the #ifdef __OBJC__block of your project's .pch file. The .pch file is a header file that is automatically #imported into every file in your project.

  1. 与其将其声明为全局变量,不如将其包装在一个单例对象中,然后在任何地方都可以使用该单例(通过 #importing .h 文件)

  2. 创建一个 .h 文件,如“Globals.h”。在 .h 中,将数组声明为extern NSMutableArray * myGlobalArray;Then 在应用程序的其他地方(AppDelegate 是个好地方),只需执行以下操作:myGlobalArray = [[NSMutableArray alloc] init];然后在任何需要数组的地方,只需#import "Globals.h"

  3. 这就像#2,但没有全局标题。您可以在项目的 .pch 文件extern NSMutableArray *myGlobalArray;#ifdef __OBJC__块内定义数组。.pch 文件是一个头文件,它会自动#imported 到项目中的每个文件中。

There are pros and cons of each approach. I've used all three at varying times in varying circumstances. I would say the singleton approach is probably the most proper, since it would be most flexible for initialization, access restriction, and memory management. However, it can be unnecessary if you don't need that.

每种方法都有优点和缺点。我在不同的情况下在不同的时间使用了这三种方法。我会说单例方法可能是最合适的,因为它在初始化、访问限制和内存管理方面最灵活。但是,如果您不需要它,则可能没有必要。

Option #2 is nice if you have lots of "global" variables that you don't want to expose to every file across your project. You can just #import it where its needed. However, this approach (as well as #3) disassociates the declaration from the initialization (ie, the object is not created near where it's declared). Some might argue this is not proper, and they might be correct.

如果您有很多不想暴露给项目中的每个文件的“全局”变量,则选项 #2 很好。你可以在需要的地方#import它。然而,这种方法(以及#3)将声明与初始化分离(即,对象不是在它声明的地方附近创建的)。有些人可能会争辩说这是不正确的,他们可能是正确的。

Option #3 is nice because then you never have to remember to #import anything at all. However, it raises the same questions as option #2.

选项 #3 很好,因为这样你就永远不必记住 #import 任何东西了。但是,它提出了与选项 #2 相同的问题。

回答by kubi

A 4th answer is to declare the array in your UIApplicationDelegateand access it through

第四个答案是在您的数组中声明数组UIApplicationDelegate并通过

[[[UIApplication sharedApplication] delegate] myArray];

For times when I just need a handful of global objects, I found this is the easiest and cleanest way to do it.

当我只需要少量全局对象时,我发现这是最简单、最干净的方法。

回答by AlBlue

If you're considering storing some kind of shared preferences for your app, use [NSUserDefaults sharedDefaults] to persist simple data which can be used across app. If you're storing transient data, then the 'static' approach will work as elsewhere.

如果您正在考虑为您的应用程序存储某种共享首选项,请使用 [NSUserDefaults sharedDefaults] 来保存可跨应用程序使用的简单数据。如果您正在存储瞬态数据,那么“静态”方法将像其他地方一样工作。

However it's probably better to use a singleton object approach with a class accessor, like NSUserDefaults, and then provide instance accessor methods to acquire your data. That way, you'll isolate yourself from potential data structure changes in the future. You'd then use a static var, as above, but within the .m file (and hence you don't need the 'extern' definition). It would typically look like:

但是,最好使用带有类访问器(如 NSUserDefaults)的单例对象方法,然后提供实例访问器方法来获取数据。这样,您就可以将自己与未来潜在的数据结构变化隔离开来。然后您将使用静态变量,如上所述,但在 .m 文件中(因此您不需要“extern”定义)。它通常看起来像:

static Foo *myDefault = nil;
@implementation Foo
+(Foo)defaultFoo {
  if(!myDefault)
    myDefault = [[Foo alloc] init]; // effective memory leak
  return myDefault;
}
@end

You'd then have instance accessors, and use them as [[Foo defaultFoo] myArray] which can be accessed from any part of the app, and without any compile time errors.

然后,您将拥有实例访问器,并将它们用作 [[Foo defaultFoo] myArray] 可以从应用程序的任何部分访问,并且没有任何编译时错误。

回答by Seva Alekseyev

Everyone here seems to have an implicit, omitted first line: "You can do it K&R C-style, or..."

这里的每个人似乎都有一个隐含的、省略的第一行:“你可以用 K&R C 风格来做,或者……”

Yes, you can still do it C-style.

是的,你仍然可以用 C 风格来做。

In file 1:

在文件 1 中:

NSArray *MyArray;

In file 2:

在文件 2 中:

extern NSArray *MyArray;

Playing Captain Obvious here.

在这里播放 Obvious 船长。

回答by Ethan Allen

In reference to Dave's answer here:

参考戴夫在这里的回答:

This is like #2, but without the global header. You can define your array as static extern NSMutableArray *myGlobalArray; inside the #ifdef OBJCblock of your project's .pch file. The .pch file is a header file that is automatically #imported into every file in your project.

这就像#2,但没有全局标题。您可以将数组定义为静态 extern NSMutableArray *myGlobalArray; 在项目的 .pch 文件的 #ifdef OBJC块内。.pch 文件是一个头文件,它会自动#imported 到项目中的每个文件中。

typedef is a storage class and static is a storage class, and you can only define objects in one storage class. Taking out "static" will allow the app to compile with your answer above.

typedef 是一个存储类,static 是一个存储类,你只能在一个存储类中定义对象。去掉“静态”将允许应用程序使用您上面的答案进行编译。

回答by Avt

There are several possibilities. Most popular:

有几种可能性。最受欢迎:

1 Use singleton object - http://www.galloway.me.uk/tutorials/singleton-classes/

1 使用单例对象 - http://www.galloway.me.uk/tutorials/singleton-classes/

2 Declare it in app delegate:

2 在应用委托中声明:

@interface Your_App_Delegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) NSArray *array;
   . . .

and access it:

并访问它:

((Your_App_Delegate*)[[UIApplication sharedApplication] delegate]).array;

3 Use NSUserDefault

3 使用NSUserDefault