top of page

In Swift an optional is a type made using an enum with two scenarios; none and some. "None" can also be represented by a special keyword "nil". "Some" has a variable attached to it. If we want to retrieve the variable then we can extract it through something called optional binding (although other options exist also).



enum Optional<T> {
    case some(T)
    case none
}
let containsAValue: Int?  = 100
let noValue: Int? = nil

Download Xcode Playground - Topic: Optionals

Xcode Playground - Optionals Workbook

Download our Optionals workbook. Opens in Xcode. The accompanying video can be found after enrolling in the 3-day course. here.



Want to try our 3-day course for free? Click here.


Optionals are explained in our 3-day online course.

Below is a rough format of how optionals are declared, checked to see if they contain a value and also extracting that value.


// 1. How to use the Optional type
let optional: Int? = nil

// 2. How to check if the variable is empty
if optional == nil {
    
}

// 3. How to check if the variable contains a value
if optional != nil {
    
}

// 4. How to extract an existing value
if let value = optional {
    print("Value: \(value)")
}

Optional Binding


Optional binding is the best method of checking to know if a value is contained within the optional as well as actually extracting it if one exists.

The if statement performs the comparison whilst also extracting the associated value and placing the contents into a newly created variable made available to the code within the if statement.


// 1. How to unwrap stored values
let exam1: Int? = 55
if let exam1Score = exam1 {
    print("Exam 1: \(exam1Score)")
}

The Problem Optionals Solve

In code we often do not have a value and at some point in the future we may populate this field. Numerous reasons exist for this, such as if a user has a middle name, or extra data has not been downloaded yet.


In the example below, we can see that an empty string is used to represent the absence of a value. However, this is not the absence of a value and is in fact a value, but empty. This empty string is a valid piece of data and would be foolish to introduce a "marker" to signal that in this event we can assume the data is missing rather than being empty (sometimes it's valid for the data to be empty).


Nil provides us with a Swift-supplied signal that our data is missing. When we have actual data (whether its empty or not) we can extract it and use it knowing that what ever it is, it is the fully downloaded and ready information.


// 1. The problem optionals solve
let stringValue: String = ""
if stringValue.count > 0 {
    print("Value: \(stringValue)")
} else {
    print("No value found")
}

Not built with A.I. A human with decades of experience wrote this article. It's designed to help you and provide some real-life guidance to start a career in the tech industry with some solid success, lots of growth and being great at what you do.


What Other Topics Do I need to Learn?

Foundational types, control flow, optionals, functions, closures, classes, structs, enums, value types, reference types, ARC (Automatic Reference Counting), extensions, protocols, concurrency, error handling and generics.


Below is the full list, available to download in markdown language.


Download


The recommended topics to study are those that each iOS Developer would be required or expected to know in order to pass an interview for a junior, mid-level or even a senior role.


Swift Study Guide

3DaysOfSwift.com | Online Swift Course


Beginner Topics

Topic 1: The Basics & Foundational Types

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics 


Topic 2: Control Flow

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow


Topic 3: Optionals

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/optionalchaining


Topic 4: Functions

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions

Closures

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/closures


Topic 5: Classes

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures


Topic 6: Structs

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures


Topic 7: Enums

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/enumerations


Topic 8: Value Types

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures#Structures-and-Enumerations-Are-Value-Types

Reference Types

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures/#Classes-Are-Reference-Types


Topic 9: ARC (Automatic Reference Counting)

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/automaticreferencecounting


Mid-level Topics

Topic 10: Extensions

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/extensions


Topic 11: Protocols

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols


Topic 12: Concurrency

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency


Topic 13: Error Handling

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/errorhandling


Topic 14: Generics

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/generics


The Swift Online Course

3DaysOfSwift.com

A 3-day online course for beginners to learn the Swift Programming Language. Each lesson is taught in Xcode; the industry-standard tool for writing software in Swift for Apple devices. This course offers transformation from beginner to Swift engineer. You build a strong foundation that can easily grow by learning more topics, more advanced features and typing more code. This short course is followed by downloading the Developers Toolkit (FREE also). All these resources are offered for FREE. Good luck on your journey learning Swift and applying for jobs in the tech industry.


Where to Learn Swift 3DaysOfSwift.com

The website starts each students learning journey with a free 3 lesson preview. All you have to do is to sign up and start learning Swift free of charge. The 4th lesson will be provided by subscribing to a monthly plan which unlocks all of the online programs including the Developers Toolkit (Language reference guides and downloadable code examples). After completing the 3-day course each student will continue to enrol in the numbered programs displayed in our members centre. The 3rd program will start the students collection of the Developers Toolkit; The Official Swift Book written by Apple, a professionally written Xcode project and The Swift Cheatsheet (language reference guide).


How to Enrol

Click here to sign up and enrol.


How to Learn Swift

Learn all topics in the Swift Study Guide, write tested systems in code alone & don't waste time building or learning UI (user interfaces). You can achieve all this at 3DaysOfSwift.com.


Why not take our online course and learn Swift in a weekend?


3DaysOfSwift.com | Email Us

🧕🏻🙋🏽‍♂️👨🏿‍💼👩🏼‍💼👩🏻‍💻💁🏼‍♀️👨🏼‍💼🙋🏻‍♂️🙋🏻‍♀️👩🏼‍💻🙋🏿💁🏽‍♂️🙋🏽‍♀️🙋🏿‍♀️🧕🏾🙋🏼‍♂️

Welcome to our community of 3DaysOfSwift.com students!


Google Search Words - SEO - Hashtags & Terms

Learn Swift. The Swift programming language. learn swift everyday. Apple. iPhone. iOS. iOS Developers. Learn to build apps. Become an iOS Developer. Career building in iOS. Write code in Swift. How to learn the swift computer programming language. How to write code in Xcode? Online course teaching Swift. Online course teaching the Swift programming language. Swift online tutorials. Learn Swift online. Swift.org. Official Swift Language Documentation. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/

We are not affiliated with 100 Days of Swift. If you want to learn SwiftUI please visit HackingWithSwift.com.

Apple developer tutorials for SwiftUI can be found here.

Copyright ©️ www.3DaysOfSwift.com. All rights reserved.

Related Posts

Comments

Commenting on this post isn't available anymore. Contact the site owner for more info.
bottom of page