Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Objective-C equivalent to Swift's fatalError?

Tags:

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?

like image 823
wj2061 Avatar asked May 17 '16 03:05

wj2061


People also ask

When to use fatalError Swift?

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.

What does fatalError do in Swift?

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.

How to handle fatal error in Swift?

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.


1 Answers

You can raise an exception in this case. Something like this:

[NSException raise:@"InitNotImplemented" format:@"Subclasses must implement a valid init method"]; 
like image 74
Shripada Avatar answered Nov 15 '22 11:11

Shripada