静态指针是强指针吗? [英] Is static pointer a strong pointer?
问题描述
在objective-c中,我知道一个静态变量(应该吗?)在程序的生命周期内保留其值.但是,如果它存储了一个指针,它在ARC中是否算作 strong ?我可以依靠它,并确保一旦将实例分配给静态变量,该实例就不会从不退出?
In objective-c, I know that a static variable (should?) retain its value for the lifetime of the program. But if it stores a pointer, does it count as strong in ARC? Can I depend on it and be assured that that instance will never go out of the heap once I assigned it to a static variable?
static ClassA* shared;
-(id)init
{
if (self=[super init]) {
shared=self;
}
return self;
}
推荐答案
是的,一旦分配,您就可以依靠它.
Yes you can rely on it, once it's assigned.
过渡到ARC发行说明状态:
在ARC中,对象类型的默认设置为强.
Under ARC, strong is the default for object types.
然后:
__ strong是默认设置.只要有强大的指针可以指向该对象,就保持其活动"状态.
__strong is the default. An object remains "alive" as long as there is a strong pointer to it.
鉴于您的static
指针引用了该对象,它将保持活动"状态.指针的范围(无论是全局指针,堆栈上的指针还是实例变量)都没有区别.
Given your static
pointer references the object, it will remain "alive". The scope of a pointer (whether global, a pointer on the stack or an instance variable) makes no difference.
这篇关于静态指针是强指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!