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.

  1. struct NuclearRocket {
  2. var meters: Double
  3. var liters: Double
  4. // Init for ??
  5. init(meters: Double, liters: Double) {
  6. self.meters = meters
  7. self.liters = liters
  8. }
  9. // Init for ??
  10. init(ft: Double, gallons: Double) {
  11. let convertedMeters = ft / 3.28
  12. let convertedLiters = gallons * 3.79
  13. self.init(meters: convertedMeters, liters: convertedLiters)
  14. }
  15. }

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

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

For American scientists

  1. var newRocket = NuclearRocket(ft: 300, gallons: 2)
  2. newRocket.liters // 7.56
  3. 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.