Difference between revisions of "W1035 Not a Value"

From Coder Merlin
Line 27: Line 27:
</syntaxhighlight>
</syntaxhighlight>


== Topic Headers ==
== Key Concepts ==
== Key Concepts ==
== Exercises ==
== Exercises ==
== References ==
== References ==

Revision as of 23:13, 7 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Paris, Notre Dame, Kilometer Null

Prerequisites[edit]

Introduction[edit]

There's often a need to represent the absence of a value. For example, consider a database tracking students' first name, middle name, and last name. Not all students have a middle name. So what value should we store in a variable representing a student's middle name if this particular student doesn't have one?

The name of this special value, representing non-existence, varies from language to language. In Swift, it's called nil.

Nil[edit]

Nil represents the absence of value of the specified type. We indicate that we intend to accept such nil values by specifying the type explicitly and suffixing the type name with a question mark. For example, in the aforementioned case, rather than specify the type as:

let middleName : String

we use:

let middleName : String?

This enables us to specify either a character or the special place-holder for no value, nil:

let middleName : String? = "A"

-or-

let middleName : String? = nil

Key Concepts[edit]

Exercises[edit]

References[edit]