Objective-C单例对象和全局变量

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

I'm aware of other posts on this topic but I'm only really one rung up the ladder from being a noob so need a bit more help.

我知道关于这个主题的其他帖子,但我只是从一个菜鸟上升的阶梯,所以需要更多的帮助。

My iPhone app has several global variables - some I have declared and given values in a class but others need to be set during a login process (like a token for example) that then need to be accessible for the lifecycle of the app from any class or method. I am told I should really be using a Singleton object for all of this which I presume is a class that's instantiated on startup. If so, could someone give me the simplest example of such header and implementation file and how/where I should instantiate it? Then I need to have some strings that are set from the off and others that can be set/got later on?

我的iPhone应用程序有几个全局变量 - 有些我已经在类中声明并给出了值,但是其他需要在登录过程中设置(例如令牌),然后需要在任何类的应用程序的生命周期中访问它们或方法。我被告知我应该使用Singleton对象来实现所有这些,我认为这是一个在启动时实例化的类。如果是这样,有人可以给我这样的标题和实现文件的最简单的例子以及我应该如何/在哪里实例化它?那么我需要有一些从off设置的字符串和其他可以在以后设置/获取的字符串?

Thanks very much in advance. Also, I'm new here so if my etiquette is off in any way, please let me know.

首先十分感谢。另外,我是新来的,所以如果我的礼仪以任何方式结束,请告诉我。

Thanks,

2 个解决方案

#1


4  

This link shows some code to create a singleton class : http://www.galloway.me.uk/tutorials/singleton-classes/

此链接显示了一些用于创建单例类的代码:http://www.galloway.me.uk/tutorials/singleton-classes/

You would use it something like :

你会用它像:

[[MyManager sharedManager] doSomething];

The call to sharedManager would get the one instance of the class (or, if this is the first time you called it, would create it) - this makes sure that you only have one of them :)

对sharedManager的调用将获得该类的一个实例(或者,如果这是您第一次调用它,则会创建它) - 这确保您只有其中一个:)

It also overrides release, retain, autorelease etc to make sure that you can't accidentally get rid of the sharedManager by mistake!

它还会覆盖release,retain,autorelease等,以确保您不会意外地错误地摆脱sharedManager!

This class will instantiate itself the first time you use it but if you need it to be created on startup, just call [MyManager sharedManager] and it will create it for you.

这个类将在你第一次使用它时实例化它,但如果你需要在启动时创建它,只需调用[MyManager sharedManager]它就会为你创建它。

You define the class like any other objective-c class - just add properties etc

您可以像任何其他objective-c类一样定义类 - 只需添加属性等

Hope that helps :)

希望有所帮助:)

#2


3  

Global variables aren't good, but singletons aren't much better when they're just used to provide global access to some data. Anything bad you can say about a global variable, you can also say about a singleton that's used for global access. A better solution is to create a data model and pass that model from one view controller to the next.

全局变量并不好,但当单例用于提供对某些数据的全局访问时,单例并不是更好。关于全局变量,你可以说任何不好的事情,你也可以说一个用于全局访问的单例。更好的解决方案是创建数据模型并将该模型从一个视图控制器传递到下一个视图控制器。

Here's a previous SO question that might help.

这是以前的SO问题可能会有所帮助。

#1


4  

This link shows some code to create a singleton class : http://www.galloway.me.uk/tutorials/singleton-classes/

此链接显示了一些用于创建单例类的代码:http://www.galloway.me.uk/tutorials/singleton-classes/

You would use it something like :

你会用它像:

[[MyManager sharedManager] doSomething];

The call to sharedManager would get the one instance of the class (or, if this is the first time you called it, would create it) - this makes sure that you only have one of them :)

对sharedManager的调用将获得该类的一个实例(或者,如果这是您第一次调用它,则会创建它) - 这确保您只有其中一个:)

It also overrides release, retain, autorelease etc to make sure that you can't accidentally get rid of the sharedManager by mistake!

它还会覆盖release,retain,autorelease等,以确保您不会意外地错误地摆脱sharedManager!

This class will instantiate itself the first time you use it but if you need it to be created on startup, just call [MyManager sharedManager] and it will create it for you.

这个类将在你第一次使用它时实例化它,但如果你需要在启动时创建它,只需调用[MyManager sharedManager]它就会为你创建它。

You define the class like any other objective-c class - just add properties etc

您可以像任何其他objective-c类一样定义类 - 只需添加属性等

Hope that helps :)

希望有所帮助:)

#2


3  

Global variables aren't good, but singletons aren't much better when they're just used to provide global access to some data. Anything bad you can say about a global variable, you can also say about a singleton that's used for global access. A better solution is to create a data model and pass that model from one view controller to the next.

全局变量并不好,但当单例用于提供对某些数据的全局访问时,单例并不是更好。关于全局变量,你可以说任何不好的事情,你也可以说一个用于全局访问的单例。更好的解决方案是创建数据模型并将该模型从一个视图控制器传递到下一个视图控制器。

Here's a previous SO question that might help.

这是以前的SO问题可能会有所帮助。