2 Data Types

WHAT YOU WILL LEARN IN THIS CHAPTER:                            

  • The basic data types: integers, floating-point numbers, and Booleans
  • The types of integers
  • How to perform integer operations
  • Different ways to represent integer literals
  • The two different floating-point types
  • How to perform floating-point operations
  • Different ways to represent floating-point literals
  • How to create type aliases
  • What tuples are
  • The new optional types
  • How to declare implicitly unwrapped optionals
  • How to perform optional binding
  • How to unwrap optionals using the ? character
  • How to define enumerations

In Chapter 1, you took a quick look at the syntax of Swift statements, as well as how to declare variables and constants quickly using type inference. In this chapter, you will learn more about the various data types available in the language.

In addition to supporting the various basic data types available in most programming languages, Swift also introduces new data types not available in Objective-C. Such new data types include the following:

  • Tuples—A tuple is a group of related values that can be manipulated as a single data type. Tuples are very useful when you need to return multiple values from a function.
  • Optional types—An optional type specifies a variable that can contain no value. Optional types make your code safer, as you will learn later in this chapter.

Swift is a type-safe language. In most cases, you have to perform explicit type conversions when assigning values from ...

Get Beginning Swift Programming 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.