如何在Objective-c中创建对所有其他类都可用的全局对象类

时间:2022-09-07 20:13:26

I would like to create some type of object or class that stores a whole bunch of variables I get from a server at one point in my application. I would then like to make this object or class available to the whole project so that I can make use or even update the variables of this object or class.

我想要创建某种类型的对象或类,它存储我在应用程序中某个时刻从服务器获得的所有变量。然后,我希望将这个对象或类提供给整个项目,这样我就可以使用甚至更新这个对象或类的变量。

I have no idea where to start with this as I am not even sure if its possible.

我不知道从哪里开始,因为我甚至不确定是否可能。

Currently the problem I am facing is that I have all of these objects passing data around different classes and I'm pretty much nesting these objects and its become very inefficient to keep track of this code. However if I had some type of class that I could just access from anywhere with all of the values up to date this would make my life a lot easier.

目前我所面临的问题是,我有所有这些对象在不同的类周围传递数据,我几乎是在嵌套这些对象,它变得非常低效,以便跟踪这些代码。但是,如果我有某种类型的类,我可以从任何地方访问,并且具有所有最新的值,这将使我的生活变得更加轻松。

Any help or suggestions would be hugely appreciated. if you need any more information please let me know.

如有任何帮助或建议,我们将不胜感激。如果你需要更多的信息,请告诉我。

4 个解决方案

#1


3  

You need to create an object that you access through this:

您需要创建通过以下方式访问的对象:

+ (MySingleton *)sharedMySingleton
{
  static dispatch_once_t shared_initialized;
  static MySingleton *shared_instance = nil;

  dispatch_once(&shared_initialized, ^ {
    shared_instance = [[MySingleton alloc] init];
  });

  return shared_instance;
}

As the comments said it is a singleton pattern. The first time you access your object it is created, then each subsequent time you get the same object when you call [MySingleton sharedMySingleton];

正如评论所说,这是一个单例模式。第一次访问对象时,它被创建,然后每次调用[MySingleton sharedMySingleton]时获得相同的对象;

#2


2  

A singleton is the way to go.

单例是一种方法。

+(SharedClass *) sharedHelper {
    static sharedClass *sharedInstance = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [sharedClass alloc];
        sharedInstance = [sharedInstance init];
    });
    return sharedInstance;
}

#3


2  

I would suggest creating a data container singleton. A singleton is an object that gets created once and only once during the life of the project. It has a class method that lets you request the object.

我建议创建一个单例数据容器。单例对象是在项目生命周期中只创建一次的对象。它有一个类方法,允许您请求对象。

Search on "Cocoa singleton design pattern" to find more about it.

搜索“Cocoa singleton设计模式”以找到更多关于它的信息。

You might have a class MyDataObject, and it might have a class method sharedDataObject. The contents of the .m file might look like this:

您可能有一个类MyDataObject,它可能有一个类方法sharedDataObject。.m文件的内容可能如下:

@implementation MyDataObject
static _sharedDataObject;

+(MyDataObject) sharedDataObject
{
  if (!_sharedDataObject)
    _sharedDataObject = [[MyDataObject alloc] init];
  return _sharedDataObject;
}

@end

Then you just add properties to the header of the data object as needed. Anywhere you need it, #import the header for the data object in your other classes, and then use code like this:

然后,只需根据需要将属性添加到数据对象的头部。在任何需要它的地方,#导入其他类中的数据对象的头,然后使用如下代码:

//To store a value to a property in the shared data object:
[MyDataObject sharedDataObject].someProperty = someValue;

//To fetch a value:
someValue = [MyDataObject sharedDataObject].someProperty;

If you need the contents of your shared data object to persist, you can make the shared data object conform to NSCoding, and then save its contents to a file when your app shifts to the background, and load its contents from a file on launch.

如果需要保留共享数据对象的内容,可以使共享数据对象符合NSCoding,然后在应用程序切换到后台时将其内容保存到文件中,并在启动时从文件中加载内容。

Note that Cocoa and Cocoa touch use a lot of singleton objects. NSUserDefaults is a singleton, and so is NSFileManager. The tip-off for a singleton is the use of a class method to return a single object. Often in Apple's frameworks the class method's name will start with "shared" or "default"

注意,Cocoa和Cocoa touch使用很多单例对象。NSUserDefaults是单例的,NSFileManager也是。单例对象的提示是使用类方法返回单个对象。在苹果的框架中,类方法的名称通常以“共享”或“默认”开头

#4


0  

Singleton is a good pattern for create one singleton object in your program, for the objects that does not interact with other singletons. If you have many singleton objects with complexity interaction, you should have more control of creation of your objects. You can use your AppDelegate, and create Singletons in ApplicationDidFinishLaunching as fields of your AppDelegate. Or you can create one Singleton object, that will contain other objects and will create other objects with necessary logic.

Singleton是在程序中为不与其他单例对象交互的对象创建单例对象的良好模式。如果您有许多具有复杂交互的单例对象,那么您应该对对象的创建有更多的控制。您可以使用AppDelegate,并在ApplicationDidFinishLaunching作为AppDelegate的字段创建单例。或者您可以创建一个Singleton对象,该对象将包含其他对象,并将创建具有必要逻辑的其他对象。

#1


3  

You need to create an object that you access through this:

您需要创建通过以下方式访问的对象:

+ (MySingleton *)sharedMySingleton
{
  static dispatch_once_t shared_initialized;
  static MySingleton *shared_instance = nil;

  dispatch_once(&shared_initialized, ^ {
    shared_instance = [[MySingleton alloc] init];
  });

  return shared_instance;
}

As the comments said it is a singleton pattern. The first time you access your object it is created, then each subsequent time you get the same object when you call [MySingleton sharedMySingleton];

正如评论所说,这是一个单例模式。第一次访问对象时,它被创建,然后每次调用[MySingleton sharedMySingleton]时获得相同的对象;

#2


2  

A singleton is the way to go.

单例是一种方法。

+(SharedClass *) sharedHelper {
    static sharedClass *sharedInstance = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [sharedClass alloc];
        sharedInstance = [sharedInstance init];
    });
    return sharedInstance;
}

#3


2  

I would suggest creating a data container singleton. A singleton is an object that gets created once and only once during the life of the project. It has a class method that lets you request the object.

我建议创建一个单例数据容器。单例对象是在项目生命周期中只创建一次的对象。它有一个类方法,允许您请求对象。

Search on "Cocoa singleton design pattern" to find more about it.

搜索“Cocoa singleton设计模式”以找到更多关于它的信息。

You might have a class MyDataObject, and it might have a class method sharedDataObject. The contents of the .m file might look like this:

您可能有一个类MyDataObject,它可能有一个类方法sharedDataObject。.m文件的内容可能如下:

@implementation MyDataObject
static _sharedDataObject;

+(MyDataObject) sharedDataObject
{
  if (!_sharedDataObject)
    _sharedDataObject = [[MyDataObject alloc] init];
  return _sharedDataObject;
}

@end

Then you just add properties to the header of the data object as needed. Anywhere you need it, #import the header for the data object in your other classes, and then use code like this:

然后,只需根据需要将属性添加到数据对象的头部。在任何需要它的地方,#导入其他类中的数据对象的头,然后使用如下代码:

//To store a value to a property in the shared data object:
[MyDataObject sharedDataObject].someProperty = someValue;

//To fetch a value:
someValue = [MyDataObject sharedDataObject].someProperty;

If you need the contents of your shared data object to persist, you can make the shared data object conform to NSCoding, and then save its contents to a file when your app shifts to the background, and load its contents from a file on launch.

如果需要保留共享数据对象的内容,可以使共享数据对象符合NSCoding,然后在应用程序切换到后台时将其内容保存到文件中,并在启动时从文件中加载内容。

Note that Cocoa and Cocoa touch use a lot of singleton objects. NSUserDefaults is a singleton, and so is NSFileManager. The tip-off for a singleton is the use of a class method to return a single object. Often in Apple's frameworks the class method's name will start with "shared" or "default"

注意,Cocoa和Cocoa touch使用很多单例对象。NSUserDefaults是单例的,NSFileManager也是。单例对象的提示是使用类方法返回单个对象。在苹果的框架中,类方法的名称通常以“共享”或“默认”开头

#4


0  

Singleton is a good pattern for create one singleton object in your program, for the objects that does not interact with other singletons. If you have many singleton objects with complexity interaction, you should have more control of creation of your objects. You can use your AppDelegate, and create Singletons in ApplicationDidFinishLaunching as fields of your AppDelegate. Or you can create one Singleton object, that will contain other objects and will create other objects with necessary logic.

Singleton是在程序中为不与其他单例对象交互的对象创建单例对象的良好模式。如果您有许多具有复杂交互的单例对象,那么您应该对对象的创建有更多的控制。您可以使用AppDelegate,并在ApplicationDidFinishLaunching作为AppDelegate的字段创建单例。或者您可以创建一个Singleton对象,该对象将包含其他对象,并将创建具有必要逻辑的其他对象。