objective c - Enforce initializing superclass's ivar after calling superclass's init method -


i need enforce initialization of ivar in superclass ivar can not initialized without other data in subclass initialized. 2 solutions have thought of is:

  1. pass required generated key ivar superclass's init method
  2. calling second superclass method subclass's init method

here example (contrived, non-working) code. stringbasedonsubclasskey ivar should initialized nsstring subclass's key method.

@interface mysuperclass : nsobject     @property (nonatomic, readonly) nsstring *stringbasedonsubclasskey; @end  @interface mysubclass : mysuperclass     @property (nonatomic, assign, readonly) int value; @end  @implementation mysubclass  - (instancetype)init {     if (self = [super init]) {         _value = 30;     }     return self; }  - (nsstring *)key {     return [nsstring stringwithformat:@"uniquekey-%d", self.value]; } 

so question is there way enforce initialization of stringbasedonsubclasskey ivar using return value of "key" method? don't believe can enforce solution 1 , 2 above. these subclasses may created other outside developers key method may more complicated this.

update: dealing existing subclasses of base class solutions limiting changes existing subclasses factor.

write getter stringbasedonsubclasskey in such way force initialization of it:

- (nsstring *) stringbasedonsubclasskey {     if !(_stringbasedonsubclasskey) {         _stringbasedonsubclasskey = // whatever;     }     return _stringbasedonsubclasskey; } 

and write superclass key method throw exception, forcing client override in subclass.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -