Swift - use of optional with let

lozflan

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?

Duncan C

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optional let and optional var inside a class in Swift

From Dev

use of let in optional value along with if

From Dev

Swift 3: 'if let' optional binding error

From Dev

" if let " must be used only for optional ? (Swift)

From Dev

Swift optional operator use

From Dev

Use of an optional value in Swift

From Dev

swift if-let doesn't unwrap optional NSDictionary

From Dev

Does "let _ = ..." (let underscore equal) have any use in Swift?

From Dev

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

From Dev

How to use if let with another statement in swift?

From Dev

Ambiguous use of "init" with optional arguments in Swift 2

From Dev

How to use optional binding in Swift 2

From Dev

If not let - in Swift

From Dev

Swift Optional of Optional

From Dev

Why doesn't using LET as an optional throw an error, as it is an implicit comparison to zero - inconsistency in Swift Intro book?

From Dev

guard let in swift 2.0 playground gets error about optional binding... why?

From Dev

Swift: when should I use "var" instead of "let"?

From Java

Why I can't use let in protocol in Swift?

From Dev

How to use guard clause in Swift and let the code more clean

From Dev

Swift: when should I use "var" instead of "let"?

From Dev

If let var - Unwrapping optional value

From Dev

How to use a protocol with optional class methods in an extension with generic in Swift?

From Dev

Swift's use of Implicitly Unwrapped Optional before availability of nullability

From Dev

What are the advantages/use cases of optional patterns introduced in swift 2?

From Dev

How to use Enums as parameters in Swift Protocols optional functions

From Dev

How to use optional chaining while searching through a dictionary in swift?

From Dev

Semantics between optional and non-optional function parameters in Swift and best use cases

From Dev

Swift: guard let vs if let

From Dev

Swift: Optional Text In Optional Value

Related Related

  1. 1

    Optional let and optional var inside a class in Swift

  2. 2

    use of let in optional value along with if

  3. 3

    Swift 3: 'if let' optional binding error

  4. 4

    " if let " must be used only for optional ? (Swift)

  5. 5

    Swift optional operator use

  6. 6

    Use of an optional value in Swift

  7. 7

    swift if-let doesn't unwrap optional NSDictionary

  8. 8

    Does "let _ = ..." (let underscore equal) have any use in Swift?

  9. 9

    Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

  10. 10

    How to use if let with another statement in swift?

  11. 11

    Ambiguous use of "init" with optional arguments in Swift 2

  12. 12

    How to use optional binding in Swift 2

  13. 13

    If not let - in Swift

  14. 14

    Swift Optional of Optional

  15. 15

    Why doesn't using LET as an optional throw an error, as it is an implicit comparison to zero - inconsistency in Swift Intro book?

  16. 16

    guard let in swift 2.0 playground gets error about optional binding... why?

  17. 17

    Swift: when should I use "var" instead of "let"?

  18. 18

    Why I can't use let in protocol in Swift?

  19. 19

    How to use guard clause in Swift and let the code more clean

  20. 20

    Swift: when should I use "var" instead of "let"?

  21. 21

    If let var - Unwrapping optional value

  22. 22

    How to use a protocol with optional class methods in an extension with generic in Swift?

  23. 23

    Swift's use of Implicitly Unwrapped Optional before availability of nullability

  24. 24

    What are the advantages/use cases of optional patterns introduced in swift 2?

  25. 25

    How to use Enums as parameters in Swift Protocols optional functions

  26. 26

    How to use optional chaining while searching through a dictionary in swift?

  27. 27

    Semantics between optional and non-optional function parameters in Swift and best use cases

  28. 28

    Swift: guard let vs if let

  29. 29

    Swift: Optional Text In Optional Value

HotTag

Archive