Is there an Objective-C equivalent to Swift's fatalError?
You can raise an exception in this case. Something like this:
[NSException raise:@"InitNotImplemented" format:@"Subclasses must implement a valid init method"];
NSAssert(NO, @"balabala");
or
- (instancetype)init NS_UNAVAILABLE;
========== Update ==========
Thanks to @Cœur
NSAssert
is disabled by default in production, which is different with FatalError
. NSException
is better.
Just call NSObject
's doesNotRecognizeSelector:
method. You would write:
- (instancetype) init
{
[self doesNotRecognizeSelector:_cmd];
}
where _cmd
is the hidden parameter to every method whose value is the selector of the method.