I’m learning Swift. On first impressions I can't see any point of declaring a constant (without an initial stored value) as an optional within a class
For example:
let userName: String?
because a default initializer would assign it to nil
and it wouldn't subsequently be able to be changed (because it's a constant).
As I understand it, a custom initializer can still assign a non-nil value to it but in that case wouldn't you just declare it as let userName: String
(i.e. non-optional)
I would have expected that if it was a redundant pattern that Apple would have made mention of it but I can't see that they have. So in what situations would an Optional
constant declaration be used or useful?
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. Once you get used to them you can actually start benefiting from them with, for example, extensions.
That value can be nil, or some other value. Once assigned it is stuck in that value. A nil is like a "this property intentionally left blank" indicator, written in permanent ink.
This makes our code easier to read than if we tried to check a condition then run some code, then check another condition and run some different code. So, use if let if you just want to unwrap some optionals, but prefer guard let if you're specifically checking that conditions are correct before continuing.
You specify optional chaining by placing a question mark ( ? ) after the optional value on which you wish to call a property, method or subscript if the optional is non- nil . This is very similar to placing an exclamation point ( ! ) after an optional value to force the unwrapping of its value.
A constant that's an optional needs to be assigned a value during the init process. That value can be nil, or some other value. Once assigned it is stuck in that value. A nil is like a "this property intentionally left blank" indicator, written in permanent ink.
Say you have a class that gets filled with response data from a network request. Some of the fields you get back may be nil, or they may contain data.
You write code that parses the response from the server and builds a response object. Each property of the response object is fixed. It either contains data if you got information for that property, or nil.
In that case it makes perfect sense to use an optional constant.
You'd write an init method for your response object that would take the network reply (In JSON, for example) and fill out the properties of the response object. If a given tag in the JSON data is missing, you'd set that property to nil. You use a constant because the value is fixed once the response object is initialized. If it's nil, it will be nil forever. If it contains a value, it will always contain that value and it can't be changed.
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