Chapter 6

Using Swift Types

In This Chapter

arrow Exploring Swift types

arrow Refining type declarations and references

arrow Using types to improve readability of code

arrow Working with tuples and optionals

In most programming languages, variables and constants not only have values, they also have types. C (the progenitor of many of today’s languages) provided four basic arithmetic types (char, int, float, and double) that can then be enhanced with optional specifiers, such as signed, unsigned, short, and long to form more complex and sophisticated types.

Adding types for variables and constants means that the compiler at runtime can perform more efficiently and can more easily check for errors. For example, in most languages, assigning “Hello” to an int type is a mistake likely to raise an error or exception at compile- or run-time. In a properly written app, every error or exception raised is a crash that’s been averted (provided that you’ve done your part by adding the necessary error- and exception-checking code to catch those errors).

Together with assertions (which are discussed at the end of ...

Get Swift For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.