How to prevent usage of other init methods other than my custom method in Objective-C

自闭症网瘾萝莉.ら 提交于 2019-12-01 12:41:14

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.

Johannes Rudolph

I usually document this and put a [self doesNotRecognizeSelector:_cmd] call into the init methods that are not intended to be used.

In conjunction, marking the method deprecated (see How do I flag a function as being deprecated in an iPhone Objective C header file?) prevents runtime suprises and gets you a warning at compile time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!