Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to handle a fail to init

Tags:

swift

ios8

I am looking for a proper way to handle a invalid argument during a initialization.

I am unsure how to do it using Swift as the init has't a return type. How can I tell whoever is trying to initialize this class that you are doing something wrong?

init (timeInterval: Int) {
    if timeInterval > 0
        self.timeInterval = timeInterval
    else
        //???? (return false?)
}

Thank you!

like image 651
Marcelo Avatar asked Jun 18 '14 22:06

Marcelo


People also ask

What does init () do in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

What is override init in Swift?

Override initializer In Swift initializers are not inherited for subclasses by default. If you want to provide the same initializer for a subclass that the parent class already has, you have to use the override keyword.

How do you initialize a class in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }

What is constructor in Swift?

In Swift, an initializer is a special function that we use to create objects of a particular class, struct, or other type. Initializers are sometimes called constructors, because they “construct” objects.


2 Answers

Use a failable initializer. Such an initializer looks very similar to a regular designated initializer, but has a '?' character right after init and is allowed to return nil. A failable initializer creates an optional value.

struct Animal {
    let species: String
    init?(species: String) {
        if species.isEmpty { return nil }
        self.species = species
    }
}

See Apple's documentation on failable initializers for more detail.

like image 149
jefe2000 Avatar answered Oct 16 '22 21:10

jefe2000


In swift, you can't really abort a task half way through execution. There are no exceptions in swift and in general the philosophy is that aborting a task is dangerous and leads to bugs, so it just should't be done.

So, you verify a value like this:

assert(timeInterval > 0)

Which will terminate the program if an invalid value is provided.

You should also change timeInterval to be a UInt so that there will be a compiler error if anybody tries to give a < 0 value or an integer value that could be < 0.

It's probably not the answer you're looking for. But the goal is to check for bad parameters as early as possible, and that means doing it before you create any objects with those parameters. Ideally the check should be done at compile time but that doesn't always work.

like image 31
Abhi Beckert Avatar answered Oct 16 '22 21:10

Abhi Beckert