Does the timing of calling the method of the super class matter in ObjectiveC?

前端 未结 2 1204
故里飘歌
故里飘歌 2021-02-06 12:44

Does it matter if I call the method of the super class first thing or at the end? For example

-(void)didReceiveMemoryWarning {
   /* do a bunch of stuff */

   [         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 13:21

    Typical Cocoa convention:

    1. If you are performing setup, call super FIRST
    2. If you are performing teardown, call super LAST

    So, initialization, viewDidLoad, etc.. fall under the first case. Memory warnings, viewDidUnload, and dealloc fall under the second case.

    You should also design your classes to follow this convention. Any deviations should be specifically noted.

    Related SO answer:

    `[super viewDidLoad]` convention


    To add: The rationale for calling super first during setup, is that you want to make sure everything is in place before you extend functionality. The corollary, is that when you are deallocating, you don't want any superclass ivars your subclass depends on to be dealloced before you have a chance to handle them.

    This makes sense in reference to UI updates as well as noted in the comments below.

提交回复
热议问题