2
The Swift Language

Swift is a new language that Apple introduced in 2014. It replaces Objective-C as the recommended development language for iOS and Mac. In this chapter, you are going to focus on the basics of Swift. You will not learn everything, but you will learn enough to get started. Then, as you continue through the book, you will learn more Swift while you learn iOS development.

Swift maintains the expressiveness of Objective-C while introducing a syntax that is safer, succinct, and readable. It emphasizes type safety and adds advanced features such as optionals, generics, and sophisticated structures and enumerations. Most importantly, Swift allows the use of these new features while relying on the same tested, elegant iOS frameworks that developers have built upon for years.

If you know Objective-C, then the challenge is recasting what you know. It may seem awkward at first, but we have come to love Swift at Big Nerd Ranch and believe you will, too.

If you do not think you will be comfortable picking up Swift at the same time as iOS development, you may want to start with Swift Programming: The Big Nerd Ranch Guide or Apple’s Swift tutorials, which you can find at developer.apple.com/​swift. But if you have some programming experience and are willing to learn on the job, you can start your Swift education here and now.

Types in Swift

Swift types can be arranged into three basic groups: structures, classes, and enumerations (Figure 2.1). All three can have:

  • properties – values associated with a type

  • initializers – code that initializes an instance of a type

  • instance methods – functions specific to a type that can be called on an instance of that type

  • class or static methods – functions specific to a type that can be called on the type itself

Figure 2.1  Swift building blocks

Figure compares the three basic Swift types: Structures, Classes, and Enumerations.

Swift’s structures (or structs) and enumerations (or enums) are significantly more powerful than in most languages. In addition to supporting properties, initializers, and methods, they can also conform to protocols and can be extended.

Swift’s implementation of typically primitive types such as numbers and Boolean values may surprise you: They are all structures. In fact, all of these Swift types are structures:

Numbers:

Int, Float, Double

Boolean:

Bool

Text:

String, Character

Collections:

Array<Element>, Dictionary<Key:Hashable,Value>, Set<Element:Hashable>

This means that standard types have properties, initializers, and methods of their own. They can also conform to protocols and be extended.

Finally, a key feature of Swift is optionals. An optional allows you to store either a value of a particular type or no value at all. You will learn more about optionals and their role in Swift later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset