ios - Objective-C call specific class method -
i have class has in initializer:
@implementation basefooclass -(id) init { if (self = [super init]) { // initialize instance variables need start value } return self; } -(id) initwithsomeint:(int) someint { if (self = [self init]) // <-- need make sure calling basefooclass's init here, not subfooclass's, make sense? { self.someint = someint; } return self; } @end
that fine , dandy. problem when implement subclass:
@implementation subfooclass -(id) init { return [self initwithsomeint:0]; } -(id) initwithsomeint:(int) someint { if (self = [super init]) // <--- infinite loop (stack overflow :) ) { // initialize other variables } } @end
i need call basefooclass
's init
rather subfooclass
's init
.
i cannot change way objects initialized, converting project c# use in ipad application.
thank in advance
edit:
due asking, here header:
@interface basefooclass : nsobject // implicit nsobject // -(id) init; -(id) initwithsomeint:(int) someint; // more methods @end @interface subfooclass : basefooclass // implicit nsobject // -(id) init; // implicit basefooclass //-(id) initwithsomeint:(int) someint; @end
objective-c doesn't work way because of way runtime converts methods function calls. self instance of allocated class, when invoking super-class's methods. need create designated initializer baseclassfoo , go there. should doing this:
@implementation basefooclass -(id) init { return [self initwithsomeint:0]; // redirect super class's designated initializer } -(id) initwithsomeint:(int) someint { if ((self = [super init])) // designated initializer calls super class's designated initializer (in case, nsobject's designated initializer init { self.someint = someint; } return self; } @end @implementation subfooclass // here don't override init because our super class's designated initializer // initwithsomeint: // -(id) init // { // return [self initwithsomeint:0]; // } // override because it's our superclass's designated initializer, plus // ours -(id) initwithsomeint:(int) someint { if ((self = [super initwithsomeint:someint])) { // initialize other sub-class specific variables } } @end
Comments
Post a Comment