What is the difference between writing
写作有什么区别
+(MyObj*)obj
{
static MyObj *obj= nil;
if(!obj)
obj = [[super allocWithZone:nil] nil];
}
and
和
+(MyObj*)obj
{
MyObj *obj= nil;
if(!obj)
obj = [[super allocWithZone:nil] nil];
}
2 个解决方案
#1
1
The storage is static
, which means there will be only one variable per process e.g. only one variable throughout iOS application.
存储是静态的,这意味着每个进程只有一个变量,例如iOS应用程序中只有一个变量。
For example, once you assign something to static
variable, the value will be there even after ending of the function. This is not the case when using local variable, which the value assigned to the variable will disappear after ending of the scope e.g. function.
例如,一旦为静态变量赋值,即使在函数结束后,该值也会存在。使用局部变量时不是这种情况,在变量范围结束后分配给变量的值将消失,例如功能。
From the second example, the obj
in second one will always be nil
at the beginning of the function because it is declared as local variable. On the other hand, obj
in the first one will be nil
at first call only because it will get assigned to new instance of MyObj
after first call.
从第二个例子开始,第二个中的obj在函数的开头总是为零,因为它被声明为局部变量。另一方面,第一个中的obj在第一次调用时将是nil,因为它将在第一次调用后被分配给MyObj的新实例。
#2
0
With the first method: Calling [WhateverClass obj] and then subsequently calling [WhateverClass obj] again will return the exact same instance of MyObj.
使用第一种方法:调用[WhateverClass obj]然后再次调用[WhateverClass obj]将返回完全相同的MyObj实例。
This is because on the first [WhateverClass obj] call the static *obj is set to nil. The if statement is invoked and *obj now points to the newly initalized instance. On the subsequent calls static *obj still points to the object from the first call (i.e. it persists between calls, and the static MyObj *obj = nil is not run). For this reason, you need to be careful with statics. Under ARC (Automatic Reference Counting) unless *obj is set back to nil again sometime in the future this object will continue to exist in memory until the process terminates.
这是因为在第一个[WhateverClass obj]调用中,static * obj被设置为nil。调用if语句,* obj现在指向新的initalized实例。在随后的调用中,static * obj仍指向第一次调用的对象(即它在调用之间持续存在,并且静态MyObj * obj = nil未运行)。因此,您需要小心静态。在ARC(自动引用计数)下,除非* obj在将来的某个时间再次设置为nil,否则此对象将继续存在于内存中,直到进程终止。
With the second method: [WhateverClass obj] and [WhateverClass obj] will return a new instance of MyObj each time. This scope is called automatic and the default behaviour is that once you exit the scope (a set of curly braces) all variables defined in the braces are lost.
使用第二种方法:[WhateverClass obj]和[WhateverClass obj]每次都会返回一个新的MyObj实例。此范围称为自动,默认行为是,一旦退出范围(一组花括号),括号中定义的所有变量都将丢失。
For further reading http://nshipster.com/c-storage-classes/ gives a pretty good overview.
如需进一步阅读,请访问http://nshipster.com/c-storage-classes/,这是一个非常好的概述。
#1
1
The storage is static
, which means there will be only one variable per process e.g. only one variable throughout iOS application.
存储是静态的,这意味着每个进程只有一个变量,例如iOS应用程序中只有一个变量。
For example, once you assign something to static
variable, the value will be there even after ending of the function. This is not the case when using local variable, which the value assigned to the variable will disappear after ending of the scope e.g. function.
例如,一旦为静态变量赋值,即使在函数结束后,该值也会存在。使用局部变量时不是这种情况,在变量范围结束后分配给变量的值将消失,例如功能。
From the second example, the obj
in second one will always be nil
at the beginning of the function because it is declared as local variable. On the other hand, obj
in the first one will be nil
at first call only because it will get assigned to new instance of MyObj
after first call.
从第二个例子开始,第二个中的obj在函数的开头总是为零,因为它被声明为局部变量。另一方面,第一个中的obj在第一次调用时将是nil,因为它将在第一次调用后被分配给MyObj的新实例。
#2
0
With the first method: Calling [WhateverClass obj] and then subsequently calling [WhateverClass obj] again will return the exact same instance of MyObj.
使用第一种方法:调用[WhateverClass obj]然后再次调用[WhateverClass obj]将返回完全相同的MyObj实例。
This is because on the first [WhateverClass obj] call the static *obj is set to nil. The if statement is invoked and *obj now points to the newly initalized instance. On the subsequent calls static *obj still points to the object from the first call (i.e. it persists between calls, and the static MyObj *obj = nil is not run). For this reason, you need to be careful with statics. Under ARC (Automatic Reference Counting) unless *obj is set back to nil again sometime in the future this object will continue to exist in memory until the process terminates.
这是因为在第一个[WhateverClass obj]调用中,static * obj被设置为nil。调用if语句,* obj现在指向新的initalized实例。在随后的调用中,static * obj仍指向第一次调用的对象(即它在调用之间持续存在,并且静态MyObj * obj = nil未运行)。因此,您需要小心静态。在ARC(自动引用计数)下,除非* obj在将来的某个时间再次设置为nil,否则此对象将继续存在于内存中,直到进程终止。
With the second method: [WhateverClass obj] and [WhateverClass obj] will return a new instance of MyObj each time. This scope is called automatic and the default behaviour is that once you exit the scope (a set of curly braces) all variables defined in the braces are lost.
使用第二种方法:[WhateverClass obj]和[WhateverClass obj]每次都会返回一个新的MyObj实例。此范围称为自动,默认行为是,一旦退出范围(一组花括号),括号中定义的所有变量都将丢失。
For further reading http://nshipster.com/c-storage-classes/ gives a pretty good overview.
如需进一步阅读,请访问http://nshipster.com/c-storage-classes/,这是一个非常好的概述。