开发者

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

Background - in my iPhone app I have a custom UITableViewController - I was going to pass some required config to it by extendi开发者_StackOverflow中文版ng the existing "(id)initWithStyle:(UITableViewStyle)style" method to an extended custom one.

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜