玩弄ARC:强制释放刺激物吗? [英] Playing around with ARC: Force release irritation?

查看:60
本文介绍了玩弄ARC:强制释放刺激物吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始进行实际工作之前,我目前正在研究ARC,以弄清楚一些事情.我确实设置了以下代码:

I am currently playing around with ARC a bit to get some things figured out, before starting to do the actual work. I did setup this code:

NSNumber* n = [[NSNumber alloc] initWithInt:3];
__weak NSNumber* weakN = n;
n = nil;
NSLog(@">>>: %@ %@", n, weakN);

我期望n和弱N为零,因为n =零;应该触发我的眼睛释放?不幸的是,事实并非如此.输出为">>> :(空)3".我在这里想念什么?

I expected n and weakN to be nil, as n = nil; should trigger a release in my eyes? Unfortunately it doesn't. The output is ">>>: (null) 3". What am I missing here?

另一件事是,我很确定,以下代码在使用arc时给我带来了困难:

Another thing is, that I am pretty sure, the below code was giving me a hard time when starting with arc:

__weak NSNumber* weakN2 = [[NSNumber alloc] initWithInt:3];
NSLog(@">>>: %@", weakN2);

我很确定,我在类似的代码上遇到了一些问题,因为在初始化之后,arc会立即释放对象,因为没有对对象的强大引用.不幸的是,上面的输出是">>>:3".

I am pretty sure, I've had some problems with similar code, as arc would release the object straight after initialization, as there is no strong reference to the object. Unfortunately, the output of the above is ">>>: 3".

弄清楚这些东西真是太好了.我显然在这里错过了一些东西!

It would be great to get some clarification on this stuff. I am clearly missing something here!

最诚挚的问候, 迈克尔

Best regards, Michael

推荐答案

除了kevboh所说的那样,创建对简单的不可变Foundation对象(如NSNumber)的弱引用也是毫无意义的.出于性能方面的考虑,Foundation可能会向您出售一个缓存的对象,而不是创建一个全新的对象.如果不是现在,那么可能会在将来的版本中发布.

In addition to what kevboh said, it's also rather pointless to create weak references to simple immutable Foundation objects like NSNumber. For performance reasons, Foundation might well vend you a cached object instead of creating an entirely new one. And if it doesn't now, then it might in some future release.

结果是,无论您怎么想,您可能不是[[NSNumber alloc] initWithInt:3]返回的对象的唯一所有者.

The upshot is that you're probably not the sole owner of the object returned by [[NSNumber alloc] initWithInt:3], no matter what you think.

这篇关于玩弄ARC:强制释放刺激物吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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