O'Reilly logo

Beginning Swift Programming by Wei-Meng Lee

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

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 ...

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