Optionals
Optionals
Optionals is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
*/The nil in the swift and the nil in objC are not the same.
The nil in the swift and the nil in Obj-C are not the same.
A nil pointer in Obj-C indicates the absence of the object. It is not a nil pointer in Swift. The nil in the swift indicates the absence of data and has support for other languages, not just the object type.
force unwrap
The variables we define as optional can be accessed using the force unwrap method.
With the exclamation operator, the code can be forced to be unwrapped, but its use, in general, is not very good.
If the reference data is not available while force unwrap, we get a runtime error.
let convertedNumber : Int? = 123
if convertedNumber != nil {
print("convertedNumber has an integer value of \(convertedNumber!).")
}
// Prints "convertedNumber has an integer value of 123."Accessing Optional with Optional Binding
Accessing Optional with Optional Binding
With the if let block, we can access the value of the optional variable by assigning it to a constant or variable.
Using this way instead of force unwrap is a more elegant method.
var possibleNumber : String? = "10"
if let actualNumber = possibleNumber {
print(actualNumber)
}example
if let firstNumber = Int("4"), let secondNumber = Int("42"),
firstNumber < secondNumber && secondNumber < 100 {
print("\(firstNumber) < \(secondNumber) < 100")
}
// Prints "4 < 42 < 100"
/*
If at least one of them is false,
it will not be entered in the if blog.
*/
Implicitly Unwrapped Optionals
Implicitly Unwrapped Optionals
In some cases, the optional value is assigned in the first stage, and in the following cases, it is absolutely not nil.
In such cases, instead of optional (?) By expressing it with the (!) operator, it is stated that this option is definitely not nil.
This form of definition is expressed as 'implicitly unwrapped optionals'.
implicitly unwrapped
implicitly unwrapped
The most used area is the class initialization phase.
Unlike traditional optional ones, we don't need to unwrap variables that are implicitly expressed as unwrapped.
let possibleString: String? = "An optional string."
// requires an exclamation mark
let forcedString: String = possibleString!
let assumedString: String! = "An implicitly unwrapped optional string."
// no need for an exclamation mark
let implicitString: String = assumedString example
If we assign nil to variables of this type, we get runtime errors.
Let's take an example
/*
We can use it during optional binding or we can check for nil.
So we use implicitly unwrapped optional
in the same way we use classic optional.
*/
var assumedString: String! = nil
if let definiteString = assumedString {
print(definiteString)
}
// Prints "An implicitly unwrapped optional string."Warning
If a variable has the potential to be nil implicity unwrapped optional is not used.
Nil-Coalescing Operator
Nil-Coalescing Operator
It provides convenience to unwrap optional types.
Let's see it as an example.
let defaultColorName = "red"
// defaults to nil
var userDefinedColorName: String?
var colorNameToUse = userDefinedColorName ?? defaultColorName
/*
userDefinedColorName is nil,
so colorNameToUse is set to the default of "red"
*/
// a != nil ? a! : b
/*
We can think of it as a shortcut to the above expression.
A checks if a is nil, if a is not nil, it unwrap and eturn,
otherwise it returns the default value, ie b.
*/
Congratulations
Congratulations 🎉
The optional concept is one of Swift's most important features. By using optional, we get rid of Nil controls and write our codes more elegantly and make our applications more secure.
See you in the next lesson 🥳

Frequently asked questions
Is the “Optionals” lesson free?
Yes — the full text of “Optionals” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Optionals”?
Optionals You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.
How long does the “Optionals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.