如何归零弱引用在非ARC下? [英] How to zeroing weak references under non-ARC?

查看:140
本文介绍了如何归零弱引用在非ARC下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢ARC。

但是ARC的最重要的功能,归零弱引用,在非ARC下缺失。目前我正在使用 MAZeroingWeakRef ,它可以正常工作,但有时候会使代码冗余。任何其他方式来归零弱引用?

But the most important feature of ARC, zeroing weak reference, is missing under non-ARC. Currently I'm using MAZeroingWeakRef, it works, but hacky, sometimes makes codes redundant. Any other ways for zeroing weak references?

推荐答案

实现归零弱引用并不困难。所有你需要做的只是跟踪所有引用指针 - 将它们存储在一个集合中,并在指针对象被释放时分配 NULL 。无论如何,手动执行所有这些操作实际上是很多工作,你真的需要编写所有手动跟踪代码以在Objective-C中足够高效。

Implementing zeroing weak reference is not hard. All what you have to do is just tracking all referencing pointers - store them in a collection - and assigning NULL when pointing object is being deallocated. Anyway, doing all these things manually is really a lot of work, you literally need to write all manual tracking code to be efficient enough in Objective-C.

最后,你会发现你需要一些自动代码编写机器 - 静态编译器 - 这正是ARC的作用。你可以自己实现像ARC。但是,如果我是你,我将只使用已经存在,稳健,稳定,精心设计和支持的编译器维护者的实现。

And at the end, you will finally discover you need some automatic code writing machine - static compiler - and that's exactly what ARC does. You could implement something like ARC yourself. But If I am you, I will just use already existing, robust, stable, well-designed and supported implementation by compiler maintainer.

行为,如果你想开发苹果的东西。与其他平台持有者(如微软)不同,苹果不在乎保持向下兼容性。如果他们不认为某物是好的,它将被弃用并最终被删除 - 如Objective-C GC。

Also, not following Apple is not wise behavior if you want to develop Apple-stuffs. Unlike other platform holders - such as Microsoft -, Apple doesn't care much about keeping backward compatibility. If they don't think something is good, it will be deprecated and removed eventually - like Objective-C GC.

这篇关于如何归零弱引用在非ARC下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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