The curious case of Optionals — #1 Emptiness

Introduction

// these could be api values from your serverlet num1: Int? = Int("123") // 123
let num2: Int? = Int("abc") // nil
let name: String = ""
let likes: Int = 0
let height: Double = 0.0
let starred: Bool = false
let feedList: [Feed] = []

Lets conclude the relationship between optional & emptiness!

#1.
let name1: String? = ""
if let name = name1, !name.isEmpty { ... }
#2.
let name2: String = ""
if !name2.isEmpty { ... }
#3.
let list: [String]? = ["a", "b", "c"]
if let list = list, !list.isEmpty { ... }
#4.
let list: [String] = ["a", "b", "c"]
if !list.isEmpty { ... }

--

--

--

iOS Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ritesh Gupta

Ritesh Gupta

iOS Developer

More from Medium

Creating a Service Layer in Swift

Drawing Vectors using UIBezierPath in Swift

Everyone can code Puzzles — Hypocycloid

Fundamental Design Pattern: ModelViewController