0Pricing
Swift Academy · Lesson

Optionals

Optionals

Introduction

Hello,

Used to express that an optional variable may not have a value.

If a variable of the optional type has a value, this variable is unwrapped and used. Or there is no value.

Optional use for many types in Swift

It can be compared to the nil pointer in Obj-C. However, nil pointer is only valid for object types.

Options in Swift are used not only for object types but also for Struct, enum, or simple types.

var optionalVar: String?
// optionalVar is automatically set to nil]
/*
Since we define it as optional without
assigning a value, the variable 
automatically takes the value of nil.
*/
← Back to Swift Academy