创建局部变量与分配给ivar和直接分配给ivar之间的区别? [英] Difference between creating a local variable and assigning to ivar and directly assigning to ivar?

查看:237
本文介绍了创建局部变量与分配给ivar和直接分配给ivar之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道为什么所有的苹果代码示例都使用这样的代码:

I have always wondered why all apple code samples use code like this:

UINavigationController *aNavigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];

self.navigationController = aNavigationController;

[self.view addSubview:[navigationController view]];

[aNavigationController release];

他们总是创建一个局部变量并将其分配给ivar,为什么他们不这样做呢?

They always create a local variable and assign it to the ivar why don't they simply do this:

self.navigationController = [[UINavigationController alloc]
          initWithRootViewController:rootViewController];;

[self.view addSubview:[navigationController view]];

[navigationController release];

除了更容易理解之外,还有其他原因吗?这是最佳做法吗?.

Is there any other reason, besides it is easier to understand?. Is this a best practice?.

-奥斯卡

推荐答案

您的替换代码不正确,因此说明了Apple试图防止的问题.这是您的代码:

Your replacement code is incorrect, and thus illustrates the problem that Apple is trying to prevent. Here is your code:

self.navigationController = [[UINavigationController alloc]
      initWithRootViewController:rootViewController];

[self.view addSubview:[navigationController view]];

[navigationController release];

您忽略了自我".在您的参考资料中.也许您打算直接访问ivar,但是在那种情况下,您通过混合访问器和直接ivar访问创建了非常混乱的代码(并且通过在访问器外部使用直接ivar访问违反了基本规则).如果没有,那么你打算写这个:

You left out the "self." in your references. Perhaps you meant to access the ivar directly, but in that case you have created very confusing code by mixing accessors and direct ivar access (and violated the cardinal rule by using direct ivar access outside an accessor). If not, then you meant to write this:

self.navigationController = [[UINavigationController alloc]
      initWithRootViewController:rootViewController];

[self.view addSubview:[self.navigationController view]];

[self.navigationController release];

最后一行是非常错误的.切勿将-release发送给方法调用的结果.因此,不,您的操作方式不正确.

That last line is very wrong. Never send -release to the result of a method call. So no, the way you're doing it isn't correct.

也就是说,我和苹果在如何执行此操作上存在分歧.这是我的方法:

That said, Apple and I disagree on how to do this. Here's how I do it:

self.navigationController = [[[UINavigationController alloc]
      initWithRootViewController:rootViewController] autorelease;

[self.view addSubview:[self.navigationController view]];

我喜欢-autorelease,因为我发现它可以防止错误.分配和释放得到的距离越远,开发人员就越有可能注入内存泄漏(例如,通过添加返回").自动释放通过将保留和释放保持在一起来避免这种情况,使使用它作为临时变量的意图更加明确,并且通常使代码审查更加容易.

I like -autorelease because I find it prevents errors. The further apart the alloc and release get, the more likely a developer will inject a memory leak (by adding a "return" for instance). autorelease avoids this by keeping the retain and release together, making the intent to use this as a temporary variable more clear, and generally makes code review much easier.

Apple倾向于在示例代码中与我不同意,因为他们通过使用发布与自动发布来强调性能.我发现这是错误的优化,因为在任何一种情况下都不会在此运行循环期间将此对象释放(因此不会节省任何内存),并且我相信自动释放的很小的性能损失已由减少由于不正确使用发行版而导致的内存泄漏.

Apple tends to disagree with me in their example code because they're emphasizing performance by using release versus autorelease. I find this to be the wrong optimization, since this object won't be deallocated during this run loop in either case (so no memory is saved), and I believe the very small performance penalty of autorelease is more than made up for by the reduction in memory leaks due to incorrect use of release.

关于自动发布与发布的争论充满了灰色阴影(我当然肯定直接在循环中使用发布),并且不同的开发人员有不同的方法,但是在两种情况下,替换代码都不是正确的方法.

The autorelease vs. release debate is filled with shades of gray (I certainly use release directly in loops), and different developers have different approaches, but your replacement code isn't the right way to do it in either case.

这篇关于创建局部变量与分配给ivar和直接分配给ivar之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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