Overriding initializer declared as NS_UNAVAILABLE by parent class

I could provide how scary I find Objective-c inheritance when it comes to initializers, but I won't. In this case, I just want to override the initializer that was previously declared as the NS_UNAVAILABLE

parent class. For example:

@interface Parent : NSObject

// Unavailable
- (instancetype)init NS_UNAVAILABLE;

// Some other (appropriate) initialiser
- (instancetype)initWithWhatever:(id)whatever NS_DESIGNATED_INITIALIZER;

@end

      

It would make sense that any calls to child initializers should propagate to the parent designated initializer. Let's say the default arguments make sense for the child class and therefore:

@interface Child : Parent

// Propagates to initWithWhatever:
- (instancetype)init NS_DESIGNATED_INITIALIZER;

@end

      

Even though the initializer has been updated with a child class xcode still seems to think it is not available. Is there a way to get around this?

+3


source to share


1 answer


It looks like this behavior has been fixed in Xcode 8 (tested in 8.1).

Also I have some notes:



  • To complete the implementation Child

    , you must override initWithWhatever

    or make it unavailable. Since Child

    , of course, this initializer inherits from Parent

    .

  • If you make it init

    unavailable, it makes sense to make it new

    unavailable.

So. We can now fully control our initializers with a combination NS_DESIGNATED_INITIALIZER

and NS_UNAVAILABLE

in Objective-C . It takes more work compared to Swift but imho it's worth it. Especially if you want your class to be better compatible with Swift.

0


source







All Articles