简体   繁体   English

在Objective C中进行子类化时需要实现的方法

[英]Methods required to be implemented when subclassing in Objective C

I am new at programming in general (though I have had a C class many, many years ago) and am learning Objective-C for programming on the iPhone. 我是编程方面的新手(尽管很多年前我已经有过很多C课程),而且我正在学习Objective-C在iPhone上进行编程。 I have what I think is a simple question, but after looking for a while (days, off and on) I can't find the answer that I'm looking for explicitly. 我认为这是一个简单的问题,但在寻找一段时间(几天,一天又一天)后,我无法找到我正在寻找的答案。

I know that when subclassing an Objective-C class I should implement the initialize method along with the deallocate method (unless using ARC for the latter, if I am correct?). 我知道,当继承Objective-C类时,我应该实现initialize方法和deallocate方法(除非对后者使用ARC,如果我是正确的?)。 The questions are: 问题是:

  1. Are these the only two to worry about, or will other classes potentially have additional methods that can be required to be implemented? 这些是唯一需要担心的两个,还是其他类可能需要实施其他方法?
  2. If other classes might have methods that I am required to implement when subclassing them, where is that documentation typically found? 如果其他类可能具有我在子类化时需要实现的方法,那么通常会找到文档在哪里? (I don't seem to see that in the Apple framework docs, though that kind of information is there for protocols it appears) (我似乎没有在Apple框架文档中看到它,尽管它出现的协议有那种信息)

Thanks for your help! 谢谢你的帮助!

  • Technically, you are not required to implement even the init and dealloc if the inherited versions are sufficient. 从技术上讲,如果继承的版本足够,则不需要实现initdealloc Also, ARC does not free you from having to write dealloc in all cases (but it certainly covers the overwhelming majority). 此外,ARC并没有让你免于在所有情况下编写dealloc (但它绝对覆盖了绝大多数)。 For example, if you allocate memory for your object using malloc , you need to free it in the dealloc . 例如,如果使用malloc为对象分配内存,则需要在dealloc释放它。
  • When you add instance variables to your class, you need to initialize them. 将实例变量添加到类时,需要初始化它们。 Typically, you do that in a designated initializer . 通常,您在指定的初始化程序中执行此操作 Again, if you do not to initialize anything, you do not have to code your own initializer; 同样,如果你不初始化任何东西,你不必编写自己的初始化程序; same goes for deinitializer. 取消初始化也是如此。
  • The only case when you need to implement a method is when you adopt a protocol with one or more methods marked @requried . 当您需要实现方法时,唯一的情况是当您采用带有一个或多个标记为@requried方法的协议时。 These methods are marked in the protocol reference. 这些方法在协议参考中标记。 For example, tableView:cellForRowAtIndexPath: and tableView:numberOfRowsInSection: are marked with the "required method" tag in Apple's documentation . 例如, tableView:cellForRowAtIndexPath:tableView:numberOfRowsInSection:Apple的文档中标有“required method”标记。

No methods are required when subclassing an NSObject (or any of their subclasses, such as UIViewController, UIView, etc. etc.). 子类化NSObject(或其任何子类,如UIViewController,UIView等)时,不需要任何方法。

If you create a new, let's say UIViewController, it's generally a good idea to keep the methods you find in the newly created file as a guideline/template, but you're not really required to keep any of the methods. 如果您创建一个新的,比如说UIViewController,通常最好将您在新创建的文件中找到的方法作为指南/模板,但实际上并不需要保留任何方法。 The super class will always call the methods on itself. 超类总是会调用方法本身。

Be aware, though, some methods you have to call super, like viewWillAppear, etc. 但请注意,您必须调用一些超级方法,例如viewWillAppear等。

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

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