how can i declare a global NSArray and then use it across my app?
如何声明全局NSArray并在应用程序中使用?
6 个解决方案
#1
26
There are a couple different ways you can do this:
有几种不同的方法可以做到这一点:
-
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)
不要将它声明为全局变量,而是将它封装在一个单例对象中,然后让单例对象在任何地方都可用(通过#导入.h文件)
-
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"
创建一个.h文件,比如“Globals.h”。在.h中,将数组声明为extern NSMutableArray * myGlobalArray;然后在应用程序的其他地方(AppDelegate是一个好地方),只需执行:myGlobalArray = [[NSMutableArray alloc] init];然后你需要数组的任何地方,只要#导入"Globals.h"
-
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.这类似于#2,但是没有全局标题。可以将数组定义为extern NSMutableArray *myGlobalArray;在项目的.pch文件的#ifdef __OBJC__块中。.pch文件是一个头文件,它将自动导入到项目中的每个文件中。
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是很好的。您可以在需要的地方导入它。但是,这种方法(以及#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很好,因为这样您就不必记住#导入任何东西。然而,它提出了与选项2相同的问题。
#2
8
A 4th answer is to declare the array in your UIApplicationDelegate
and 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.
当我只需要一些全局对象时,我发现这是最简单、最干净的方法。
#3
2
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,然后提供实例访问器方法来获取数据。这样,您就可以将自己与未来可能发生的数据结构更改隔离开来。然后使用静态var,如上面所示,但是在.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],可以从应用程序的任何部分访问它们,并且不会出现任何编译时错误。
#4
1
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 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.
这类似于#2,但是没有全局标题。可以将数组定义为静态extern NSMutableArray *myGlobalArray;在项目的.pch文件的#ifdef OBJC块中。.pch文件是一个头文件,它将自动导入到项目中的每个文件中。
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是一个存储类,并且您只能在一个存储类中定义对象。删除“静态”将允许应用程序编译您上面的答案。
#5
1
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船长。
#6
0
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在app委托中声明:
@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使用nsuserdefaults
#1
26
There are a couple different ways you can do this:
有几种不同的方法可以做到这一点:
-
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)
不要将它声明为全局变量,而是将它封装在一个单例对象中,然后让单例对象在任何地方都可用(通过#导入.h文件)
-
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"
创建一个.h文件,比如“Globals.h”。在.h中,将数组声明为extern NSMutableArray * myGlobalArray;然后在应用程序的其他地方(AppDelegate是一个好地方),只需执行:myGlobalArray = [[NSMutableArray alloc] init];然后你需要数组的任何地方,只要#导入"Globals.h"
-
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.这类似于#2,但是没有全局标题。可以将数组定义为extern NSMutableArray *myGlobalArray;在项目的.pch文件的#ifdef __OBJC__块中。.pch文件是一个头文件,它将自动导入到项目中的每个文件中。
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是很好的。您可以在需要的地方导入它。但是,这种方法(以及#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很好,因为这样您就不必记住#导入任何东西。然而,它提出了与选项2相同的问题。
#2
8
A 4th answer is to declare the array in your UIApplicationDelegate
and 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.
当我只需要一些全局对象时,我发现这是最简单、最干净的方法。
#3
2
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,然后提供实例访问器方法来获取数据。这样,您就可以将自己与未来可能发生的数据结构更改隔离开来。然后使用静态var,如上面所示,但是在.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],可以从应用程序的任何部分访问它们,并且不会出现任何编译时错误。
#4
1
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 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.
这类似于#2,但是没有全局标题。可以将数组定义为静态extern NSMutableArray *myGlobalArray;在项目的.pch文件的#ifdef OBJC块中。.pch文件是一个头文件,它将自动导入到项目中的每个文件中。
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是一个存储类,并且您只能在一个存储类中定义对象。删除“静态”将允许应用程序编译您上面的答案。
#5
1
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船长。
#6
0
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在app委托中声明:
@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使用nsuserdefaults