Two Phase Init

Introduction

Welcome to Lesson 6 of Object Oriented Swift. You will learn how one init method may initialize the other. The process is analogous to that of convenience. At the end, you will learn how the NASA could have saved millions of dollars if they used the feature Swift offers.

Problem

How can one init init the other?

Design Rocket Ship

Design a struct called NuclearRocket. There are two init methods and two properties: meters and liters.

struct NuclearRocket {
  var meters: Double
  var liters: Double

  // Init for 🇰🇷
  init(meters: Double, liters: Double) {
    self.meters = meters
    self.liters = liters
  }

  // Init for 🇺🇸
  init(ft: Double, gallons: Double) {
    let convertedMeters = ft / 3.28
    let convertedLiters = gallons * 3.79
    self.init(meters: convertedMeters, liters: convertedLiters)
  }
}

Using the second init method, you may initialize the properties using ft and gallon. Yet, you've initalized the meters and liters properties by callingself.init.

Create Object

For Korean scientists

var rocket = NuclearRocket(meters: 20, liters: 20)
rocket.liters // 20
rocket.meters // 20

For American scientists

var newRocket = NuclearRocket(ft: 300, gallons: 2)
newRocket.liters // 7.56
newRocket.meters // 91.4

Source Code

2006-two-phase-init.playground

Conclusion

In 1998, The NASA launched The Mars Climate robot to study the Mars. However, a year later, it went out of the orbit. It was due to the computer software which produced non-SI units. The program returned units of pound instead of the SI units of newton. $327.6 million evaporated. They could have potentially used two phase initializations in Swift.

In the following lesson, you will learn how to use methods and properties without creating an actual object.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.

results matching ""

    No results matching ""