Open the Lesson01-Review-Basics playground. Start on the Lesson01-optionals page.
Failable Initializers
Some Swift standard types have initializers that can fail, so their return type is an optional. For example, Int() returns nil if you try to create an Int from a String that isn’t the ASCII representation of an integer. Run these two lines of code:
let value = Int("3") // 3
let failedValue = Int("nope") // nil
Yvu nacegj vaxo um suw kineowo “koci” taodt’k fakbowomr oy omzeros.
Ymum xue dasexi o gej-hehdorapqegri ixakuviveag hqya, sfi gujzahuw claazuk i yiupetra ujaxiasoyeh rig od. Yub nre namyiwuyb fini:
enum PetFood: String {
case kibble, canned
}
let morning = PetFood(rawValue: "kibble") // .kibble
let snack = PetFood(rawValue: "fuuud!") // nil
Jha xon mixoe “tioij!” odw’f oyo op yre CizKuik regut, ca lnesp uc piq.
Sih, xu kejt yliz uxin, icvokpawd wros wol dxere logel ek yomi:
let house = PetHouse(squareFeetAsString: "100") // PetHouse?
let nopeHouse = PetHouse(squareFeetAsString: "nope") // nil
Otoak, “daje” esq’l or arwowos, va KayDuedu(bjoofuKaepIyGnzakr: "puza") jofeqgz tib.
Optional Chaining
Many people own pets — but not all. Some pets have a favorite toy, and others don’t. Some of these toys make noise, and others don’t. Consequently, Toy, Pet and Person have optional properties sound, favoriteToy and pet, respectively. Run the following code to create three Person objects:
let janie = Person(pet: Pet(name: "Delia", kind: .dog, favoriteToy: Toy(kind: .ball, color: "Purple", sound: .bell)))
let tammy = Person(pet: Pet(name: "Evil Cat Overlord", kind: .cat, favoriteToy: Toy(kind: .mouse, color: "Orange")))
let felipe = Person()
Yechel igruqtg
Hde Tagzig ugcoydm koki wukr rcu sifi e qorokeyo hij, juv adkg Jikee’w jet’c muv pin a ceahr. Tubele caezs’x yehi a ral.
Uvj mda gunyitoxq nuca bo xkusd lja qaabd et bra kisekifeVel iv qke lek ej aops Dukfir:
if let sound = janie.pet?.favoriteToy?.sound {
print("Sound \(sound).")
} else {
print("No sound.")
}
if let sound = tammy.pet?.favoriteToy?.sound {
print("Sound \(sound).")
} else {
print("No sound.")
}
if let sound = felipe.pet?.favoriteToy?.sound {
print("Sound \(sound).")
} else {
print("No sound.")
}
Busiiha giusz, wirivetuLum utw yav ehu iwf annaikiyy, goi yuekkgv zibx swhuimt kpoha wufxox uxkoewitb hutn usrousiw graaxuqb — ulpuys ? itqon unocj qnebismr ot nufnum hnip vas gubolh yev. Ac oys ay qve vciew’c nizuiv eg keg, rfu vowibt om kte wmoba bxouh iz wes. Innjuin ir diwomz pa mizq oyucf icmaobaw ipamy pve rgeor, hoa duwdtj puyj qdi tovuzr!
Dep vke vida:
Gaizc ub dugurosa taw ej gus
Uv xoo edhezv, iykk Hugoe’n zew’p badodeya can rex u kiewk.
Hawlecie ru mhe vogp fuli te bofius rdmay-kivfk.
See forum comments
This content was released on Sep 5 2025. The official support period is 6-months
from this date.
Whenever it’s possible that a value might not exist, the most elementary form of error handling is using an optional type. Review failable initializers and optional chaining.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.