I want to discard superclass's default init method.I can achieve this easily with fatalError
in Swift:
class subClass:NSObject{ private var k:String! override init(){ fatalError("init() has not been implemented") } init(kk:String){ k = kk } }
How can I do it in Objective-C?
The fatalError() function has a special return type called Never , which Swift understands as meaning execution will never continue after this function has been called. As a result, you can use fatalError() in methods that return a value but you have nothing sensible to return.
As the name implies, fatal errors are a type of error. They are typically thrown when the application enters an unknown or unexpected state. As a result, the behavior of the application becomes undefined. The behavior of the application becomes undefined.
There are four ways to handle errors in Swift. You can propagate the error from a function to the code that calls that function, handle the error using a do - catch statement, handle the error as an optional value, or assert that the error will not occur.
You can raise an exception in this case. Something like this:
[NSException raise:@"InitNotImplemented" format:@"Subclasses must implement a valid init method"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With