Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在多个类中为变量分配单例是一种很好的编程实践吗_Objective C_Design Patterns_Singleton_Automatic Ref Counting - Fatal编程技术网

Objective c 在多个类中为变量分配单例是一种很好的编程实践吗

Objective c 在多个类中为变量分配单例是一种很好的编程实践吗,objective-c,design-patterns,singleton,automatic-ref-counting,Objective C,Design Patterns,Singleton,Automatic Ref Counting,在我的应用程序中,我使用的是singleton类(作为sharedInstance)。当然,我需要在多个类(视图控制器)中使用存储在该单例中的数据。 因为写作 [[[SingletonClass sharedInstance]arrayWithData]计数]或 [[[SingletonClass sharedIntanse]arrayWithData]objectAtIndex:index]或者您在数组上使用的一些其他方法不太合适。我想,在非singleton类的生命周期开始时,将该非sing

在我的应用程序中,我使用的是singleton类(作为sharedInstance)。当然,我需要在多个类(视图控制器)中使用存储在该单例中的数据。 因为写作

[[[SingletonClass sharedInstance]arrayWithData]计数]

[[[SingletonClass sharedIntanse]arrayWithData]objectAtIndex:index]
或者您在数组上使用的一些其他方法不太合适。我想,在非singleton类的生命周期开始时,将该非singleton类的属性(强的、非原子的)指定为与SingletonClass具有相同的地址

self.arrayPropertyOfOtherClassOne=[[SingletonClass sharedInstance]arrayWithData]
在别的班上

self.arrayPropertyOfOtherClassTwo=[[SingletonClass sharedInstance]arrayWithData]

这是好的编程实践吗

在我看来,没有什么不好的。属性将指向与Singleton中的属性相同的地址,非Singleton类之后也将销毁指向Singleton的属性,因此引用计数=引用计数-1

我说得对吗

在我看来,没有什么不好的

通常,您希望维护一个指向单例的指针,而不是指向它包含的某个对象。通过保留指向它所包含的对象的指针,您添加了一个更难的依赖项和一个要求,即该对象不会更改或以某种定义的方式更改。如果您已经定义并记录了这一点,那么它应该是可以的,但是,通常情况下,singleton应该能够根据需要销毁该对象并创建一个新对象,因此您可能需要重新考虑保留对它的引用

保留对singleton本身的引用是很好的,因为它永远不会被破坏

属性将指向与Singleton中的属性相同的地址

真的

而非单例类之后也将销毁指向单例的属性,所以引用计数=引用计数-1

真的