在弧线中,当你不合成时会发生什么 [英] In arc what happens when you don't synthesize
问题描述
在启用iOS ARC的项目中,当我不合成属性时会发生什么,因为不允许保留/释放?
In an iOS ARC enabled project, what happens when I don't synthesize a property, since retain/release are disallowed?
@interface SomeClass : NSObject {
NSMutableArray* _pieces;
}
@end
iVar _pieces的内存语义是什么这个案例?
假设我使用 _pieces = what
设置它。
What are the memory semantics of the iVar _pieces in this case?
Say I set it using, _pieces = whatever
.
实例时_pieces设置为nil我的SomeClass被解除分配?
_pieces是否存储为弱引用?
如果保留_pieces的所有其他对象都释放它,当我尝试访问它时它是否为null?
Is _pieces set to nil when the instance of my SomeClass is deallocated? Is _pieces stored as a weak reference? If all other objects that have retained _pieces release it, will it be null when I attempt to access it?
推荐答案
一些观察结果,其中很多可能基于其他人的反馈而明白:
A couple of observations, much of which is probably clear by this point based upon the feedback of others:
-
您合成了属性,而不是实例变量,在您的示例中,您向我们展示了一个实例变量的示例,而不是属性。
You synthesize properties, not instance variables, and in your example, you showed us an example of an instance variable, not a property.
您的问题可能意味着在合成和执行保留
/ 发布
,但没有这样的连接。执行保留
和发布
的能力取决于您是否使用ARC 与否。它与合成属性无关。
Your question might imply some assumed connection between synthesizing and the ability to do retain
/release
, but there is no such connection. The ability to do retain
and release
is a function of whether you are using ARC or not. It has nothing to do with synthesizing properties.
正如其他人所观察到的,显式声明的实例变量(例如您的示例)是 strong
默认情况下引用。因此,在您的示例中, _pieces
是 strong
参考。
As others have observed, explicitly declared instance variables, such as your example, are strong
references, by default. So, in your example, _pieces
is a strong
reference.
是的,当您的 SomeClass
对象被取消分配时,它将删除其 strong
对<的引用code> _pieces 对象。显然,如果这是 _pieces
指向的对象的最后一个强引用,它将被释放,并且任何其他弱
引用你必须在其他地方将其设置为 nil
。有关内存管理的更完整讨论,请参阅Apple的高级内存管理编程指南和过渡到ARC 。
Yes, when your SomeClass
object is deallocated, it will remove its strong
reference to the _pieces
object. Obviously, if that's the last strong reference to the object pointed to by _pieces
it will be deallocated and any other weak
references you have to it elsewhere will be set to nil
. For a more complete discussion on the memory management, see Apple's Advanced Memory Management Programming Guide and Transitioning to ARC.
你问如果所有其他保留 _pieces
的对象都释放它,它会是 nil
当我尝试访问它时?显然,如果 _pieces
是一个弱
引用,那将是真的,但鉴于它隐含了一个
引用,不,情况并非如此。 SomeClass
中的强
You asked "If all other objects that have retained _pieces
release it, will it be nil
when I attempt to access it?" Obviously that would be true if _pieces
was a weak
reference, but given that it's implicitly a strong
reference in SomeClass
, no, that is not the case.
如果你想制作件
a 声明属性,语法为
@property(非原子,强)NSMutableArray * pieces;
指定 strong
与弱
(或其他)指示内存管理属性。
If you wanted to make pieces
a declared property, the syntax would be
@property (nonatomic, strong) NSMutableArray* pieces;
The designation of strong
vs. weak
(or whatever) dictates the memory management of the property.
如果你声明一个属性,你不仅不再需要显式定义实例变量,而是现在建议你真的不应该这样做(因为当它被合成时,编译器将为你创建ivar)。但是,如果您碰巧有一个显式声明的属性正确名称的实例变量,编译器将使用该属性。但这不仅是不必要的,而且也是不可取的(因为如果你错误输入实例变量的名称,你可能会在不知不觉中最终得到两个实例变量)。只需让编译器为您的属性合成您的实例变量,这种潜在的歧义就会消失。
If you declare a property, you not only no longer have to explicitly define the instance variable, but rather it is now advised that you really should not do so (because when it's synthesized, the compiler will create the ivar for you). But, if you happen to have an explicitly declared instance variable of the right name for your property, the compiler will use that for the property. But that's not only unnecessary, but also inadvisable (because if you mistype the name of the instance variable, you may unwittingly end up with two instance variables). Just let the compiler synthesize your instance variables for your properties and this potential ambiguity goes away.
将为属性合成的实例变量的名称是由属性实现指令,即 @synthesize
语句。因此,如果您有件
属性的 @synthesize
语句:
@synthesize pieces;
然后实例变量将被称为 pieces
。但是如果你使用首选的 @synthesize
语法:
@synthesize pieces = _pieces;
然后实例变量名将具有前面的下划线(按照惯例,这是首选的,以避免代码在属性和实例变量之间出现歧义)。并且,从Xcode 4.4开始,如果省略 @property
的 @synthesize
语句,它将隐式合成它对于后一种语法,即实例变量将带有前导下划线。
The name of the instance variable that will be synthesized for a property is governed by the syntax of the property implementation directive, i.e. the @synthesize
statement. Thus, if you have a @synthesize
statement for your pieces
property of the form:@synthesize pieces;
then the instance variable will be called pieces
. But if you use the preferred @synthesize
syntax of:@synthesize pieces = _pieces;
then the instance variable name will have the preceeding underscore (which is, by convention, preferred, to avoid ambiguity in your code between properties and instance variables). And, as of Xcode 4.4, if you omit the @synthesize
statement for a @property
, it will implicitly synthesize it for you with the latter syntax, i.e. the instance variable will bear the leading underscore).
这篇关于在弧线中,当你不合成时会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!