ios - Overwriting and custom inits in objective-c -


i have class inherits mpmovieplayerviewcontroller. i'm trying rewrite init method don't have repeat code every other -initwithsomething function. custom initwithsomething method work. can't figure how make work inherited iniwithsomething methods

-(instancetype)init {     if(self = [super init]){         // code don't want repeat in initwithsomething methods         [self startwithhiddencontrols];          avaudiosession *audiosession = [avaudiosession sharedinstance];         [audiosession setcategory:avaudiosessioncategoryplayback error:nil];         [audiosession setactive:yes error:nil];          [[nsnotificationcenter defaultcenter] removeobserver:self                                                         name:uiapplicationdidenterbackgroundnotification                                                       object:nil];          [[nsnotificationcenter defaultcenter] addobserver:self                                                  selector:@selector(uiapplicationdidbecomeactive)                                                      name:uiapplicationdidbecomeactivenotification                                                    object:nil];     }     return self; }  -(instancetype)initwithcontenturl:(nsurl *)contenturl {      // not overwriting doesn't use overwriten init must it.     // how ?!?     // why [super init] makes inifinite loop? (i know norm [super <same method>])     if(self = [self init]){} // not work. inifinite loop believe.      return self; }  //this method works fine - (instancetype)initwithsettings:(nsdictionary *)settings {     // [self init] works here how?     if(self = [self init]){         //my custom stuff     } } 

during writin post found [super init] calls -(instancetype)initwithcontenturl:(nsurl *)contenturl , there lies infinite loop problem. why designated initializer init calls secondary initializer initwithurl? should not other way around? please explain.

does mean should put code, don't want repeated, in initwithurl method instead of init method.

edit: , did. entered code in initwithurl method. both default init , custom init run it.

why call init designated initializer? believe, not. let's refer the docs:

the designated initializer plays important role class. ensures inherited instance variables initialized invoking designated initializer of superclass. it typically init... method has parameters , of initialization work, , initializer secondary initializers of class invoke messages self.

so doesn't seem satisfy both characteristic: has least number of parameters , apparently calls initializer (initwithcontenturl) self. so, believe, if found real designated initializer, work expected.

according mpmovieplayerviewcontroller class reference, designated initializer initwithcontenturl. can override instead of init.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -