在Objective-C中使用太多的静态变量是不好的做法吗?

时间:2022-09-07 19:29:37

Will usage of static variables expose them to a danger of being modifiable from anywhere ?(In context of Objective-C). If yes, can someone suggest best alternatives for using shared variables across all classes ?

静态变量的使用是否会使它们在任何地方都可能被修改?如果是,有人能建议跨所有类使用共享变量的最佳替代方法吗?

2 个解决方案

#1


4  

Is using too many static variables in Objective-C a bad practice?

在Objective-C中使用太多的静态变量是不好的做法吗?

Yes. Of course, "too many" has not been quantified and is subjective. Really, global/static variables are very rarely a good thing -- very convenient to introduce and very difficult to debug and eliminate. Also rare is the case that they are good design. I've found life far easier without them.

是的。当然,“太多”并没有被量化,而是主观的。实际上,全局/静态变量很少是好事——引入非常方便,调试和消除也非常困难。同样稀有的是,它们都是好的设计。我发现没有他们的生活要容易得多。

Will usage of static variables expose them to a danger of being modifiable from anywhere? (In context of Objective-C).

静态变量的使用会使它们暴露在任何地方都可以修改的危险中吗?(在objective - c的上下文中)。

It depends on where they are declared and how they are used. If you were to pass a reference to another part of the program, then they would be modifiable from 'anywhere'.

这取决于它们在哪里声明以及如何使用。如果您将引用传递到程序的另一部分,那么它们将从“任何地方”进行修改。

Examples:

例子:

If you place them so that only one file can "see" the variable (e.g. in a .m file following all includes), then only the succeeding implementation may use it (unless you pass a reference to the outside world).

如果您放置它们以便只有一个文件可以“看到”变量(例如,在包含之后的.m文件中),那么只有后续的实现可以使用它(除非您将引用传递给外部世界)。

If you declare the variable inside a function, then it is shared among each translation and copied for each translation in C/ObjC (but the rules are very different in C++/ObjC++).

如果在函数中声明变量,那么它将在每个翻译之间共享,并在C/ObjC中为每个翻译复制(但是在c++中规则非常不同)。

If yes, can someone suggest best alternatives for using shared variables across all classes?

如果是,有人能建议跨所有类使用共享变量的最佳替代方法吗?

Just avoid using globals altogether. Create one or more type/object to hold this data, then pass an instance of it to your implementations.

避免使用全局变量。创建一个或多个类型/对象来保存此数据,然后将其实例传递给实现。

Singletons are the middle ground, in that you have some type of global variable/object based abstraction. Singletons are still so much hassle -- they are categorized as global variables and banned in my codebase.

单例是中间地带,因为您有某种类型的基于全局变量/对象的抽象。单例仍然有很多麻烦——它们被归类为全局变量,在我的代码库中被禁止。

#2


3  

Static variables are local to the translation unit, so the variables are definitely not modifiable from anywhere. Globals, which are closely related to statics in that they are allocated in the same memory area, are modifiable from anywhere, and that's their main danger.

静态变量在转换单元中是局部的,所以这些变量在任何地方都是不可修改的。全局变量与静态变量密切相关,因为它们被分配到相同的内存区域,可以从任何地方修改,这是它们的主要危险。

When you need a group of variables to be accessible from anywhere in your project, the common approach is implementing a singleton that holds related data, and contains methods for processing that data. In MVC apps implemented in Objective C the model is often accessed through a singleton model object.

当您需要从项目中的任何位置访问一组变量时,通常的方法是实现一个包含相关数据的单例,并包含处理数据的方法。在Objective - C中实现的MVC应用程序中,通常通过单例模型对象访问模型。

My scenario involves a number of static variables declared in the .h file & they are assigned values in specific methods declared in those .h files.

我的场景涉及在.h文件中声明的许多静态变量,它们在那些.h文件中声明的特定方法中被赋值。

If you declare statics in the header, they become "disconnected" from each other: each translation unit (i.e. each .m file) gets its own set of statics from the header. This is usually not what you want.

如果在header中声明statics,它们就会彼此“断开连接”:每个翻译单元(即每个.m文件)从header中获得自己的静态集。这通常不是你想要的。

If you make these variables global, you end up with a plain C, not an Objective C, solution. You should put these variables in a class as properties, and move function implementations with them into the methods of your class. Then make the class a singleton as described in the answer linked above to get a solution that is easier to understand than the corresponding solution based on globals.

如果你让这些变量成为全局变量,你最终会得到一个简单的C,而不是一个Objective - C,解决方案。您应该将这些变量作为属性放在类中,并将函数实现连同它们一起移动到类的方法中。然后,按照上面链接的答案中的描述,将类设置为单例,以获得比基于全局的相应解决方案更容易理解的解决方案。

#1


4  

Is using too many static variables in Objective-C a bad practice?

在Objective-C中使用太多的静态变量是不好的做法吗?

Yes. Of course, "too many" has not been quantified and is subjective. Really, global/static variables are very rarely a good thing -- very convenient to introduce and very difficult to debug and eliminate. Also rare is the case that they are good design. I've found life far easier without them.

是的。当然,“太多”并没有被量化,而是主观的。实际上,全局/静态变量很少是好事——引入非常方便,调试和消除也非常困难。同样稀有的是,它们都是好的设计。我发现没有他们的生活要容易得多。

Will usage of static variables expose them to a danger of being modifiable from anywhere? (In context of Objective-C).

静态变量的使用会使它们暴露在任何地方都可以修改的危险中吗?(在objective - c的上下文中)。

It depends on where they are declared and how they are used. If you were to pass a reference to another part of the program, then they would be modifiable from 'anywhere'.

这取决于它们在哪里声明以及如何使用。如果您将引用传递到程序的另一部分,那么它们将从“任何地方”进行修改。

Examples:

例子:

If you place them so that only one file can "see" the variable (e.g. in a .m file following all includes), then only the succeeding implementation may use it (unless you pass a reference to the outside world).

如果您放置它们以便只有一个文件可以“看到”变量(例如,在包含之后的.m文件中),那么只有后续的实现可以使用它(除非您将引用传递给外部世界)。

If you declare the variable inside a function, then it is shared among each translation and copied for each translation in C/ObjC (but the rules are very different in C++/ObjC++).

如果在函数中声明变量,那么它将在每个翻译之间共享,并在C/ObjC中为每个翻译复制(但是在c++中规则非常不同)。

If yes, can someone suggest best alternatives for using shared variables across all classes?

如果是,有人能建议跨所有类使用共享变量的最佳替代方法吗?

Just avoid using globals altogether. Create one or more type/object to hold this data, then pass an instance of it to your implementations.

避免使用全局变量。创建一个或多个类型/对象来保存此数据,然后将其实例传递给实现。

Singletons are the middle ground, in that you have some type of global variable/object based abstraction. Singletons are still so much hassle -- they are categorized as global variables and banned in my codebase.

单例是中间地带,因为您有某种类型的基于全局变量/对象的抽象。单例仍然有很多麻烦——它们被归类为全局变量,在我的代码库中被禁止。

#2


3  

Static variables are local to the translation unit, so the variables are definitely not modifiable from anywhere. Globals, which are closely related to statics in that they are allocated in the same memory area, are modifiable from anywhere, and that's their main danger.

静态变量在转换单元中是局部的,所以这些变量在任何地方都是不可修改的。全局变量与静态变量密切相关,因为它们被分配到相同的内存区域,可以从任何地方修改,这是它们的主要危险。

When you need a group of variables to be accessible from anywhere in your project, the common approach is implementing a singleton that holds related data, and contains methods for processing that data. In MVC apps implemented in Objective C the model is often accessed through a singleton model object.

当您需要从项目中的任何位置访问一组变量时,通常的方法是实现一个包含相关数据的单例,并包含处理数据的方法。在Objective - C中实现的MVC应用程序中,通常通过单例模型对象访问模型。

My scenario involves a number of static variables declared in the .h file & they are assigned values in specific methods declared in those .h files.

我的场景涉及在.h文件中声明的许多静态变量,它们在那些.h文件中声明的特定方法中被赋值。

If you declare statics in the header, they become "disconnected" from each other: each translation unit (i.e. each .m file) gets its own set of statics from the header. This is usually not what you want.

如果在header中声明statics,它们就会彼此“断开连接”:每个翻译单元(即每个.m文件)从header中获得自己的静态集。这通常不是你想要的。

If you make these variables global, you end up with a plain C, not an Objective C, solution. You should put these variables in a class as properties, and move function implementations with them into the methods of your class. Then make the class a singleton as described in the answer linked above to get a solution that is easier to understand than the corresponding solution based on globals.

如果你让这些变量成为全局变量,你最终会得到一个简单的C,而不是一个Objective - C,解决方案。您应该将这些变量作为属性放在类中,并将函数实现连同它们一起移动到类的方法中。然后,按照上面链接的答案中的描述,将类设置为单例,以获得比基于全局的相应解决方案更容易理解的解决方案。