Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use optionals and when should I use non-optionals with default values?

I know the recommended way in Swift is to use:

class Address {
var firstLine : String?
var secondLine : String?
}

but sometimes I see other developers write their code this way:

class Address {
var firstLine : String = ""
var secondLine : String = ""
}

Is this the unrecommended way because whenever you have nil you will just crash and there's no outlet for your to recover. Is that right? Or there are some use cases where using non-optionals with default can be good. If so then where?

I saw this other question which is asking about efficiency rather than which better suits your needs. I'm looking for an answer where it says "This is a good place to use non-optionals and this is a good place to use optionals". Sometimes I see folks just dump optionals everywhere and it makes me think do we not ever need non-optionals? Sometimes I see people trying to avoid optionals as much as possible and just code in an Objective-C kind of style.

The above question's answer doesn't represent a valid case for where non-optionals are good. It's mute about that. As for choosing optionals: I'm guessing for models which get populated by network calls, optionals are the right choice, because you don't know whether it's nil or not.

like image 920
mfaani Avatar asked May 31 '17 15:05

mfaani


People also ask

When should you use optionals Swift?

Optional types or Optionals in Swift You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn't a value at all. That's a pretty straightforward definition.

Why do we need optionals in Swift?

Optionals are in the core of Swift and exist since the first version of Swift. An optional value allows us to write clean code with at the same time taking care of possible nil values. If you're new to Swift you might need to get used to the syntax of adding a question mark to properties.

What should you use to provide a default value for an optional type variable in Swift?

Use the nil-coalescing operator ( ?? ) to supply a default value in case the Optional instance is nil .

What is optional and non optional in Swift?

Non-optional allows us to declare variables without optional and without initial value but we have to assign a value before using it other compile-time error. It can't be nil. *No default value. We can't assign nil to any non-optional variable. will get a compile-time error. During initialization.


1 Answers

The choice depends on what you model.

If a property of the object that you model may be absent completely, e.g. a middle name, a name suffix, an alternative phone number, etc., it should be modeled with an optional. A nil optional tells you that the property is not there - i.e. a person does not have a middle name or an alternative phone number. You should also use optional when you must distinguish between an empty object and a missing object.

If a property of the object must be set, and has a meaningful default, use an non-optional with a default:

class AddressList {
    var addresses : [Address]
    var separator : String = ";"
    ...
}

If users of your class need to change the separator, they have a way to do that. However, if they do not care about the separator, they can continue using the default without mentioning it in their own code.

like image 81
Sergey Kalinichenko Avatar answered Nov 15 '22 07:11

Sergey Kalinichenko