Difference between revisions of "W1035 Not a Value"

From Coder Merlin
 
Line 29: Line 29:
== Nil Complexities ==
== Nil Complexities ==
While '''nil''' is very helpful in these cases, using the value in an expression becomes slightly more complex, because for each such value, we need to take into account the ''possibility'' that it may be '''nil'''.  Let's consider another example:
While '''nil''' is very helpful in these cases, using the value in an expression becomes slightly more complex, because for each such value, we need to take into account the ''possibility'' that it may be '''nil'''.  Let's consider another example:
<syntaxhighlight lang="swift" highlight="3">
<syntaxhighlight lang="swift" line highlight="3">
let a : Double = 0.4
let a : Double = 0.4
let b : Double? = nil
let b : Double? = nil
Line 36: Line 36:
Line 3 will result in an error: <span style="color:red">value of optional type 'Double?' must be unwrapped to a value of type 'Double'</span>
Line 3 will result in an error: <span style="color:red">value of optional type 'Double?' must be unwrapped to a value of type 'Double'</span>
Why? Because we assumed that b was not '''nil'''.  Since it wouldn't make any sense to add '''nil''' to a Double, we need to ensure that b is, in fact, not '''nil''' before attempting to use it as an addend.  We'll learn later various ways to check whether or not an optional contains '''nil''', but, ''if we are certain that a value is not '''nil''', we can indicate this with an exclamation point ''at the end'' of the name of the optional:
Why? Because we assumed that b was not '''nil'''.  Since it wouldn't make any sense to add '''nil''' to a Double, we need to ensure that b is, in fact, not '''nil''' before attempting to use it as an addend.  We'll learn later various ways to check whether or not an optional contains '''nil''', but, ''if we are certain that a value is not '''nil''', we can indicate this with an exclamation point ''at the end'' of the name of the optional:
<syntaxhighlight lang="swift" highlight="3">
<syntaxhighlight lang="swift" line highlight="3">
let a : Double = 0.4
let a : Double = 0.4
let b : Double? = 0.2
let b : Double? = 0.2

Latest revision as of 23:53, 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

Types which may be nil are termed optionals, because the constant or variable might have a value of the specified type; if not, it will be nil.

Nil Complexities[edit]

While nil is very helpful in these cases, using the value in an expression becomes slightly more complex, because for each such value, we need to take into account the possibility that it may be nil. Let's consider another example:

let a : Double = 0.4
let b : Double? = nil
let c = a + b

Line 3 will result in an error: value of optional type 'Double?' must be unwrapped to a value of type 'Double' Why? Because we assumed that b was not nil. Since it wouldn't make any sense to add nil to a Double, we need to ensure that b is, in fact, not nil before attempting to use it as an addend. We'll learn later various ways to check whether or not an optional contains nil, but, if we are certain that a value is not nil, we can indicate this with an exclamation point at the end of the name of the optional:

let a : Double = 0.4
let b : Double? = 0.2
let c = a + b!

Note that unwrapping refers to removing the "box" around the optional to reveal its contents, which should contain a value of the underlying type. If we are force-unwrapping, by using the exclamation point, our program will terminate with an error if the optional contained nil.

Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png
  • There's often a need to represent the absence of a value.
  • The name of this special value, representing non-existence, varies from language to language.
  • In Swift, it's called nil
  • 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.
  • Types which may be nil are termed optionals
  • In most cases, before using optionals, we must unwrap them to obtain their value
  • If we force unwrap an optional which is nil, our program will terminate

Exercises[edit]

References[edit]