O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bronze Challenge

Optionals are best used for things that can literally be nil; that is, they are useful in representing the complete absence of something. But nonexistence is not the same as zero. For example, if you are writing code to model a bank account, and the user has no balance in a given account, the value 0 is more appropriate than nil. In other words, the user does not lack an account; what they are missing is money! Take a look at the examples below and select which type would model them best.

  • The number of kids a parent has: Int or Int?

  • The name of a person’s pet: String or String?

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required