简体   繁体   English

什么是便利初始化程序,因为它与Objective-C有关?

[英]What is a convenience initializer as it pertains to Objective-C?

I started learning programming about 8 months ago, started with C, OOP, now onto iOS, which is my goal. 我大约8个月前开始学习编程,从C,OOP开始,现在开始学习iOS,这是我的目标。 Everything is going pretty smooth for the most part and I've started to practice by programming small applications on xcode. 在大多数情况下,一切都很顺利,我已经开始通过在xcode上编写小应用程序来练习。 It's just little terms like convenience initializer that sometimes throw me off. 它只是像便利初始化器这样的小术语,有时会让我失望。 Can anyone define this term for me and just give me a quick example of its usage? 任何人都可以为我定义这个术语,并给我一个快速的例子来说明它的用法吗? I understand the concept of allocation and initialization but I am unable to differentiate between an initializer and a convenience initializer. 我理解分配和初始化的概念,但我无法区分初始化器和便利初始化器。 I've looked online but not much luck. 我看过网上但运气不多。

Any help is appreciated, thanks 任何帮助表示赞赏,谢谢

Basically, a convenience initializer/constructor is a class (static / non member) method, that returns an instance of an class. 基本上,便捷初始化器/构造函数是一个类(静态/非成员)方法,它返回类的实例。

It means that, in order to get an actual instance of a class, you may use a convenience constructor (or initializer), if one is provided, rather that allocating the object explicitly. 这意味着,为了获得类的实际实例,您可以使用便捷构造函数(或初始化程序)(如果提供了一个),而不是显式分配对象。

It will replace the standard alloc / init (or initWith... ) way. 它将取代标准的alloc / init (或initWith... )方式。

But... 但...

In terms of memory management, it has a completely different meaning! 在内存管理方面,它有着完全不同的含义!

You don't own an object returned by a convenience constructor. 您没有便利构造函数返回的对象。
It means you don't have to release it by yourself. 这意味着您不必自己发布它。

For instance, when you allocate a NSMutableArray : 例如,当您分配NSMutableArray

NSMutableArray * a = [ [ NSMutableArray alloc ] initWithCapacity: 10 ];

You called alloc. 你叫alloc。 It means you'll have to release the object, as you own it: 这意味着您必须释放该对象,因为您拥有它:

NSMutableArray * a = [ [ NSMutableArray alloc ] initWithCapacity: 10 ];

/* Some stuff here... */

[ a release ];

It's not the case with a convenience constructor, as the object will be released automatically (it's automatically placed on the current auto-release pool): 方便构造函数不是这样,因为对象将自动释放(它会自动放在当前的自动释放池中):

NSMutableArray * a = [ NSMutableArray arrayWithCapacity: 10 ];

/* Some stuff here... */
/* No need to release the array! */

Basically, here's what the arrayWithCapacity convenience constructor of NSMutableArray does: 基本上,这是NSMutableArrayarrayWithCapacity便利构造函数所做的:

+ ( NSMutableArray * )arrayWithCapacity: ( NSUInteger )capacity
{
    return [ [ [ NSMutableArray alloc ] initWithCapacity: capacity ] autorelease ];
}

Note the call to autorelease at the end? 注意最后调用autorelease
It means the object will be released automatically at the end of the current run loop, so you don't have to do it by yourself (if you don't retain it explicitly, of course). 这意味着对象将在当前运行循环结束时自动释放,因此您不必自己完成(当然,如果您没有明确地保留它)。

Final note 最后的说明

You did not mention if you are using ARC or not. 你没有提到你是否使用ARC
I assumed it's not the case. 我认为情况并非如此。

Of course, if you use ARC , this is completely different, as the retain/release are done automatically (most of the time), based on variables qualifiers (strong/weak). 当然,如果你使用ARC ,这是完全不同的,因为保留/释放是基于变量限定符(强/弱)自动(大多数时间)完成的。

But even if using ARC , you should know how reference counting works. 但即使使用ARC ,您也应该知道引用计数的工作原理。

I had the same doubt, but solved now: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MultipleInitializers.html 我有同样的疑问,但现在解决了: https//developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MultipleInitializers.html

Note: if the above link failed, read this backup: https://gist.github.com/JeOam/9116926 注意:如果上述链接失败,请阅读此备份: https//gist.github.com/JeOam/9116926

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

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