Assign value with optional question mark

Peterses

I'm currently learning Swift from basics. Now I'm on optionals and I'm trying to understand this case:

var text: String? = nil

text? = "some text"

What happens exactly if we assign value with question mark? I don't understand why the value of text is nil. Can you explain me what is the difference between assigning text = "some text" and text? = "some text"?

matt

You're right to be surprised. text? = means "If text is nil don't perform the assignment."

You've stumbled across a highly obscure language feature. See https://ericasadun.com/2017/01/25/pretty-much-every-way-to-assign-optionals/ (As she rightly says, you can count on the fingers of zero hands the number of times you'll ever actually talk like this, because who would ever want to assign a value only just in case the lvalue is already non-nil?)

NOTE I prefer to look at this as a zero-length variant of optional chaining. It is extremely useful to be able to say e.g.

self.navigationController?.hidesBarsOnTap = true

meaning, if self.navigationController is nil, fuhgeddaboudit. Well, your use case is sort of a variant of that, with nothing after the question mark. Most people are unaware that if the last object in the chain is an Optional, the chain can end in a question mark. The expressions in f are all legal:

class C {
    struct T {
        var text : String?
    }
    var t : T?
    func f() {
        self.t?.text = "howdy"
        self.t? = T()
        self.t?.text? = "howdy"
    }
}

But only the first one, self.t?.text =, is common to say in real life.

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 chaining question mark after function name

From Dev

It it okay to access an optional variable with question mark without exclamation mark?

From Dev

Assign value to optional dictionary in Swift

From Dev

Assign value of Optional to a variable if present

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

Why there is a ? mark in if-let statement to see an optional has a value

From Dev

Drop rows with a 'question mark' value in any column in a pandas dataframe

From Dev

jOOQ addConditions: in SQL question mark appears instead of the value

From Java

What is the default value of the nullable type "int?" (including question mark)?

From Dev

Laravel query returns question mark instead of variable value

From Dev

during cast of a char value in java question mark is printed on console

From Dev

What's wrong with my rails value assignment containing a question mark?

From Dev

Mark a Less mixin as optional

From Dev

Swift optional variable assignment with default value (double question marks)

From Dev

Python, assign function to variable, change optional argument's value

From Dev

assign null default value for optional query param in route - Play Framework

From Dev

Question mark on a type

From Dev

JdbcTemplate - replace question mark

From Dev

Parameter with question mark and super

From Dev

Question Mark in Directive Require

From Dev

Question mark in callback

From Dev

COUNTIF with a question mark symbol

From Dev

Question mark in SAS

From Dev

Remove ASCII Question Mark

From Dev

Question mark in htaccess

From Dev

Hebrew is question mark in html

From Dev

question mark keyword in Haskell

From Dev

Question mark at the end of the output

From Dev

Matching a question mark in .htaccess

Related Related

  1. 1

    Optional chaining question mark after function name

  2. 2

    It it okay to access an optional variable with question mark without exclamation mark?

  3. 3

    Assign value to optional dictionary in Swift

  4. 4

    Assign value of Optional to a variable if present

  5. 5

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

  6. 6

    Why there is a ? mark in if-let statement to see an optional has a value

  7. 7

    Drop rows with a 'question mark' value in any column in a pandas dataframe

  8. 8

    jOOQ addConditions: in SQL question mark appears instead of the value

  9. 9

    What is the default value of the nullable type "int?" (including question mark)?

  10. 10

    Laravel query returns question mark instead of variable value

  11. 11

    during cast of a char value in java question mark is printed on console

  12. 12

    What's wrong with my rails value assignment containing a question mark?

  13. 13

    Mark a Less mixin as optional

  14. 14

    Swift optional variable assignment with default value (double question marks)

  15. 15

    Python, assign function to variable, change optional argument's value

  16. 16

    assign null default value for optional query param in route - Play Framework

  17. 17

    Question mark on a type

  18. 18

    JdbcTemplate - replace question mark

  19. 19

    Parameter with question mark and super

  20. 20

    Question Mark in Directive Require

  21. 21

    Question mark in callback

  22. 22

    COUNTIF with a question mark symbol

  23. 23

    Question mark in SAS

  24. 24

    Remove ASCII Question Mark

  25. 25

    Question mark in htaccess

  26. 26

    Hebrew is question mark in html

  27. 27

    question mark keyword in Haskell

  28. 28

    Question mark at the end of the output

  29. 29

    Matching a question mark in .htaccess

HotTag

Archive