简体   繁体   English

是否需要释放initWithNibName

[英]does initWithNibName need to be released

I use initWithNibName to load detail views. 我使用initWithNibName加载详细信息视图。 I was just thinking do these things need to be released at all? 我只是想这些东西需要发布吗? init is basically adding a retain count of 1? init基本上是添加1的保留计数?

Yes. 是。 Read this section . 阅读本节 Anything starting with init... gives you an object you own. 任何以init...开头的东西都会给你一个你拥有的对象。

@Mel: @Mel:

Yes you need to release them. 是的,你需要释放它们。

A Part from Apple's Doumentation: 来自Apple的Doumentation的一部分:

You own any object you create 您拥有自己创建的任何对象

You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy). 使用名称以“alloc”,“new”,“copy”或“mutableCopy”开头的方法(例如,alloc,newObject或mutableCopy)创建对象。

But as such it has nothing to do with initWithNibName but it is related to four words which allocate the memory that is 'mutableCopy','copy', 'retain' and 'alloc'. 但是因为它与initWithNibName无关,但它与四个单词有关,这四个单词分配的内容是'mutableCopy','copy','retain'和'alloc'。 So hope it is clear to you now. 所以希望你现在很清楚。

init keyword just initializes an object. init关键字只是初始化一个对象。 Allocation of memory is done though alloc or retain or copy or mutableCopy keyword 内存分配是通过allocretaincopymutableCopy关键字完成的

And the retain count of 1 that you are talking about is because of the alloc keyword, not the initWithNibName . 你所说的保留计数为1是因为alloc关键字,而不是initWithNibName

Hope this helps you. 希望这对你有所帮助。

The section in question: 有问题的部分:

You own any object you create You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy). 您拥有自己创建的任何对象使用名称以“alloc”,“new”,“copy”或“mutableCopy”开头的方法(例如,alloc,newObject或mutableCopy)创建对象。

You can take ownership of an object using retain A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. 您可以使用retain获取对象的所有权通常保证接收到的对象在接收到的方法中保持有效,并且该方法也可以安全地将对象返回给其调用者。 [...] [...]

When you no longer need it, you must relinquish ownership of an object you own You relinquish ownership of an object by sending it a release message or an autorelease message. 当您不再需要它时,您必须放弃您拥有的对象的所有权您通过向对象发送释放消息或自动释放消息来放弃对象的所有权。 In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object. 因此,在Cocoa术语中,放弃对象的所有权通常被称为“释放”对象。

You must not relinquish ownership of an object you do not own This is just corollary of the previous policy rules, stated explicitly. 您不得放弃您不拥有的对象的所有权这只是之前明确规定的政策规则的必然结果。

Generally, though, you should avoid thinking in terms of retain counts and focus on ownership . 但是,一般来说,您应该避免考虑保留计数并关注所有权 If you own it, it's up to you to release it. 如果您拥有它,则由您来释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM