简体   繁体   English

创建新的指定初始值设定项时,是否必须覆盖init?

[英]Must you override init when you create a new designated initializer?

I have read up quite a bit on designated and convenience initializers in objective-c and feel I have a good understanding of how and why they are used. 我已经在objective-c对指定和便利初始化器进行了相当多的阅读,并且我对它们的使用方式和原因有了很好的理解。 I just have a couple of outstanding questions, well one really. 我只有几个突出的问题,其中一个是真的。

Say you have a class that inherits directly from NSObject and you create a designated initializer for it. 假设您有一个直接从NSObject继承的类,并为它创建一个指定的初始值设定项。

I realise you can now mark your designated initializer using NS_DESIGNATED_INITIALIZER but I am wondering if that means it forces you to separately, within the same class, first override init with a call to your designated initializers? 我意识到你现在可以使用NS_DESIGNATED_INITIALIZER标记你的指定初始值设定项,但是我想知道这是否意味着它会强制你在同一个类中分别通过调用指定的初始化程序来覆盖init?

Secondly, if you weren't to use this macro, what might the implications be of NOT overriding init explicitly with the call to the designated initializer? 其次,如果你不使用这个宏,那么在调用指定的初始值设定项时,NOT明确覆盖init的含义是什么呢? I realise the compiler would not know which was the designated initializer, so could anything bad come of this, and is it just good practice to also override init when you create a new designated initalizer for your class? 我意识到编译器不会知道哪个是指定的初始化程序,所以可能有什么不好的,并且当你为你的类创建一个新的指定的initalizer时,它是否也是优秀的做法?

EDIT: I just want to add further clarification on this question. 编辑:我只想进一步澄清这个问题。 I have seen examples of good coders not adding a distinct method to override init despite having added a designated initialiser of their own making. 我已经看到好的编码器的例子没有添加一个不同的方法来覆盖init,尽管已经添加了他们自己制作的指定初始化器。 When I ask if you should override init I mean in addition to creating your own specific designated initialiser which of course should call its superclass's init method. 当我问你是否应该覆盖init时我的意思是除了创建你自己的特定指定初始化器,当然应该调用它的超类的init方法。 I have seen a good coder not do this and so I wondered why you would therefore do it. 我见过一个好的程序员没有这样做,所以我想知道为什么你会这样做。

you can now mark your designated initializer using NS_DESIGNATED_INITIALIZER but I am wondering if that means it forces you to separately, within the same class, first override init with a call to your designated initializer? 您现在可以使用NS_DESIGNATED_INITIALIZER标记您指定的初始化程序,但我想知道这是否意味着它会强制您在同一个类中单独覆盖init,并调用指定的初始化程序?

Calling any initializer in self will bypass the warning. 在self中调用任何初始化程序将绕过警告。 When I try to call a super.init method I do get the following warning. 当我尝试调用super.init方法时,我收到以下警告。

Convenience initializer missing a 'self' call to another initializer. 方便初始化程序错过了对另一个初始化程序的“自我”调用。

what might the implications be of NOT overriding init explicitly with the call to the designated initializer? 通过调用指定的初始化程序,NOT显式重写init会带来什么影响呢?

This would mean your object might be missing some important information that it requires to function, or that the app assumes the object always has, and if it doesn't have it, it could cause errors or a crash in your app. 这意味着您的对象可能缺少它需要运行的一些重要信息,或者该应用程序假设该对象始终具有该对象,如果它没有它,则可能导致应用程序中的错误或崩溃。

Here is a good explanation of how it works: 以下是对其工作原理的一个很好的解释:

http://timekl.com/blog/2014/12/09/objective-cs-designated-secret/ http://timekl.com/blog/2014/12/09/objective-cs-designated-secret/

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

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