ARC项目中用于NSTextView的IBOutlet [英] IBOutlet for NSTextView in a ARC project

查看:95
本文介绍了ARC项目中用于NSTextView的IBOutlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处中,在大多数情况下,IBOutlet应该较弱.

As you read here in most cases a IBOutlet should be weak.

现在您可以在

Now as you can read in the development library not all classes support weak references. (e.g. NSTextView). This means you have to use assign:

@property (assign) IBOutlet NSTextView *textView;

如果使用弱引用,则会出现以下错误: 不允许合成弱的不可用属性,因为它需要合成__weak对象的ivar".

If you use a weak reference you will get the following error: "Synthesis of a weak-unavailable property is disallowed because it requires synthesis of an ivar of the __weak object"

文档中遗漏的内容是,您现在必须在使用该属性后再次将其设置为nil,例如通过dealloc方法:

What the documentation missed to mention is now you have to set the property again to nil after it's usage e.g. by a dealloc method:

- (void)dealloc
{
    self.textView = nil;
} 

据我了解,标有NS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLE的类不支持弱引用,但原因是什么?

As far as I understood classes marked with NS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLE don't support weak references but what is the reason?

推荐答案

我想我找到了某些类不支持弱引用的原因:

I think I found the reason why some classes don't support a weak reference:

您可以阅读此处:

合理的价格:从历史上看,一个类有可能通过重写保留,释放等来提供其自己的引用计数实现.但是,对对象的弱引用需要与其类的引用计数实现进行协调,因为对于最终版本,事物,弱负载和存储必须是原子的.因此,现有的自定义引用计数实现通常不付出额外的努力就不会支持弱引用.在不破坏二进制兼容性的情况下,这是不可避免的.

Rationale: historically, it has been possible for a class to provide its own reference-count implementation by overriding retain, release, etc. However, weak references to an object require coordination with its class's reference-count implementation because, among other things, weak loads and stores must be atomic with respect to the final release. Therefore, existing custom reference-count implementations will generally not support weak references without additional effort. This is unavoidable without breaking binary compatibility.

这篇关于ARC项目中用于NSTextView的IBOutlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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