在这种情况下,“释放"在iPhone上意味着什么? [英] What does 'release' means in this situation on iPhone?

查看:83
本文介绍了在这种情况下,“释放"在iPhone上意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于iPhone应用程序的愚蠢问题.我是iPhone应用程序的绿色代表.我在Apple网站上阅读了以下代码.

I want to ask a stupid question about the iPhone application. I am the green of the iPhone app. I read the following code in the Apple website.

    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
    [self setMyViewController:aViewController];
    [aViewController release];

我有一个问题,第3行中的发布"是什么意思?

And I have a question, how it means of the 'release' in the line 3?

它可以清除内存吗?还是程序控制了那个对象?或其他含义.非常感谢.

Does it presents the memory clear? or the program take control of that object? or other meanings. Thank you very much.

推荐答案

当您alloc某物时,所获得的对象的保留计数为1-这意味着该对象当前正在被某人使用,因此不应从内存中删除.如果在对象上调用retain,它将增加保留计数,这意味着该对象正在被2种事物使用.如果保留计数达到0,则表示该对象不再被任何对象使用,可以将其从内存中删除.您可以通过在对象上调用release来减少对象的保留计数.

When you alloc something, the object you get will have a retain count of 1 - this means that this object is currently being used by someone, so it should not be removed from memory. If you call retain on an object it will increase the retain count, meaning the object is being used by 2 things. If the retain count reaches 0, it implies that the object is no longer being used by anything and it can be removed from memory. You can decrease an object's retain count by calling release on the object.

在您的示例中,aViewControlleralloc,第1行之后的保留计数为+1.

In your example, aViewController is alloc'd and after line 1 has a retain count of +1.

然后将其设置为第2行中的视图控制器.为此,该方法获得对象的所有权,因此retain应该将其自己使用.

It is then set as the view controller in line 2. This method is therefor taking ownership of the object, so should retain it for its own use.

第3行,我们不希望与视图控制器有任何关系,因此我们将其保留为release.保留计数减少一-现在,由新所有者将其完成后释放.

Line 3, we don't want anything more to do with the view controller, so we release our hold of it. The retain count decreases by one - and it is now up to the new owner to release it when it is finished with it.

通读此教程

这篇关于在这种情况下,“释放"在iPhone上意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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