我有一个循环参考。如何在Objective-C中创建弱引用? [英] I have a circular reference. How can I create a weak reference in Objective-C?

查看:115
本文介绍了我有一个循环参考。如何在Objective-C中创建弱引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究iPhone应用程序。我有一个类 Row 的对象需要释放类 Block 的大量对象。每个 Block 目前都有一个属性,该属性保留类 Row 的实例变量。

I'm working on an iPhone application. I have an object of class Row that needs to release numerous objects of the class Block. Every Block currently has a property that retains an instance variable of class Row.

@interface Block : UIImageView {
  Row *yCoord;
}
@property (nonatomic,retain) Row *yCoord;
@end

包含这些块的 NSMutableArray

@interface Row : NSObject {
    NSMutableArray *blocks;
}
-(void)addBlock:(Block*)aBlock;
@end

@implementation Row
-(void)addBlock:(Block*)aBlock {
    [blocks addObject:aBlock];
    aBlock.yCoord = self;
}
@end

我知道这是一个循环引用。 Apple的文档声明,为了使用循环引用释放对象,我需要一个弱引用而不是强引用(一个保留属性),但它没有贯彻并解释我究竟是如何做到的。我计划同时释放和释放一行中的所有块以及行本身。如何在每个块中将弱引用设置为父行?

I understand that this is a circular reference. Apple's documentation states that in order to deallocate an object with a circular reference I need a weak reference instead of a strong reference (a retain property), but it doesn't follow through and explain how exactly I go about doing so. I plan to release and dealloc all Blocks within a Row as well as the Row itself simultaneously. How do I set up a weak reference within each of my Blocks to their "parent" Row?

推荐答案

编辑: 由于提问者澄清他没有使用垃圾收集(iPhone目前不支持垃圾收集),我的建议是通过让一个对象保留另一个对象来避免周期,就像你对委托一样。使用属性时,使用assign而不是retain来实现此目的。例如:

Since the asker clarified he's not using garbage collection (iPhone currently does not support it), my advice is to avoid cycles by having only one of the objects retain the other, just as you would do with a delegate. When using properties, use "assign" instead of "retain" to achieve this. For example:

@property (nonatomic,assign) Row *yCoord;

我的答案的其余部分与Objective-C 2.0和GC的弱引用有关。

The rest of my answer answer relates to "weak references" in terms of Objective-C 2.0 and GC.

当您使用垃圾收集(10.5+)时,通过为变量声明添加前缀来创建弱引用 __弱 。当您分配给该变量时,GC(如果已启用)会跟踪引用,并且如果对引用对象的所有强引用都消失,则会自动将其归零。 (如果未启用GC,则忽略 __ weak 属性。)

When you're working with garbage collection (10.5+), a weak reference is created by prefixing a variable declaration with __weak. When you assign to that variable, the GC (if enabled) keeps track of the reference and will zero it out for you automatically if all strong references to the referenced object disappear. (If GC is not enabled, the __weak attribute is ignored.)

因此,您可以安全地修改上述内容回答玩垃圾收集更好(目前在10.5以上,也许有一天在iPhone上)如下:(参见相关的Apple文档。)

Thus, you can safely modify the above answer to play nicer with garbage collection (currently on 10.5+, and perhaps someday on iPhone) as follows: (See the related Apple docs.)

@property (nonatomic,assign) __weak Row *yCoord;

引用 Chris Hanson (在那里可以找到更多详细信息):

To quote Chris Hanson (where you can find more detailed information):


通过为实例变量加前缀使用 __ weak 进行声明,告诉垃圾收集器,如果它是对象的唯一引用,则该对象应被视为可收集。

"By prefixing an instance variable declaration with __weak, you tell the garbage collector that if it's the only reference to an object that the object should be considered collectable."

我通过说如果没有对象的非弱引用来澄清这一点。一旦删除了最后一个强引用,就可以收集该对象,并且所有弱引用都将自动归零。

I'd clarify that by saying "if there are no non-weak references to an object". As soon as the last strong reference is removed, the object may be collected, and all weak references will be zeroed automatically.

注意:这不是直接与创建弱引用有关,但是还有 __ strong 属性,但是自Objective-C默认情况下,对象变量是强引用,它通常仅用于原始C指针,例如垃圾收集器不会将其视为根的结构或基元,如果不将它们声明为强,则将从您下面收集。 (缺少 __弱会导致保留周期和内存泄漏,缺少 __ strong 会导致内存陷入困境,非确定性地发生的非常奇怪和阴险的错误,并且很难追查。)

Note: This isn't directly related to creating weak references, but there is also a __strong attribute, but since Objective-C object variables are strong references by default, it is generally used only for raw C pointers to things like structs or primitives that the Garbage Collector will not treat as roots, and will be collected from under you if you don't declare them as strong. (Whereas the lack of __weak can cause retain cycles and memory leaks, the lack of __strong can result in memory stomping and really strange and insidious bugs that occur non-deterministically and can be quite difficult to track down.)

这篇关于我有一个循环参考。如何在Objective-C中创建弱引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆