CocoaObjective-C中的全局变量?

CocoaObjective-C中的全局变量?,第1张

概述根据Cocoa Programming for Mac OS X,第3版,第202页(第13章): You will be registering, reading, and setting defaults in several classes in your application. To make sure that you always use the same name, you sho 根据Cocoa Programming for Mac OS X,第3版,第202页(第13章):

You will be registering,reading,and
setting defaults in several classes in
your application. To make sure that
you always use the same name,you
should declare those strings in a
single file and then simply #import
that file into any file in which you
use the names. There are several ways
to do this. For example,you Could use
the C preprocessor’s #define command,
but most Cocoa programmers use global
variables for this purpose.

这真的是正确的最佳做法吗?全局变量?这对我来说似乎很疯狂 – 反对我曾经教过的一切。

一个更好的设计是一个简单的Singleton类与这些定义?还是真正做好全球化的最佳做法?有没有比任何一个更好的模式,因为许多人认为Singleton是一个漂亮的衣服全球化?

解决方法 全局变量或单例将在此完成相同的事情。两个可以用来转换Cocoa中的“key”名称,如果它拼写错误,编译器错误就不会抛出编译器错误。这是主要目的。全局变量看起来更简单,因为它需要较少的打字。

而不是这样做:

[myArray setobject:theObject forKey:MyGlobalVariableKeyname];

你必须做以下一些事情:

[myArray setobject:theObject             forKey:[[MySingletonVariableClass getInstance] myVariableKeyname];

全局变量本质上较少地输入相同的效果。

总结

以上是内存溢出为你收集整理的Cocoa/Objective-C中的全局变量?全部内容,希望文章能够帮你解决Cocoa/Objective-C中的全局变量?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1039397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存