如何防止在objective-c中使用除我的自定义方法之外的其他init方法 [英] how to prevent usage of other init methods other than my custom method in objective-c

查看:112
本文介绍了如何防止在objective-c中使用除我的自定义方法之外的其他init方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景 - 在我的iPhone应用程序中,我有一个自定义的UITableViewController - 我将通过将现有的(id)initWithStyle:(UITableViewStyle)样式方法扩展到扩展的自定义方法来传递一些必需的配置。

Background - in my iPhone app I have a custom UITableViewController - I was going to pass some required config to it by extending the existing "(id)initWithStyle:(UITableViewStyle)style" method to an extended custom one.

问题 - 确保此自定义控制器类的用户只能调用我的自定义init方法而不是initWithStyle或任何其他init方法的最佳方法是什么?

Question - what's the best way to ensure that the user of this custom controller class can only call my custom init method, and not initWithStyle or any other init methods?

推荐答案

您可以覆盖不想使用的init方法,并在那里引发异常。

You can override the init methods that you don't want to be used, and raise an exception there.

您也可以覆盖它们并使用指定的初始值设定项进行初始化。

You can also override them and make them initialize with the designated initializer.

此外,您应该在文档中指定它。

Also, you should specify it on your documentation.

这篇关于如何防止在objective-c中使用除我的自定义方法之外的其他init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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