Contained in: Swift Apprentice: Fundamentals
swift
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Expressions, Variables & Constants
Apr 23 2025 · Chapter
…your whirlwind introduction to the world of programming! You’ll begin with an overview of computers and programming and then say hello to Swift playgrounds, where you’ll spend your coding time for the rest of this book. You’ll learn some basics, such as code comments, arithmetic operations, constants…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Types & Operations
Apr 23 2025 · Chapter
…convert it to another. The naïve way to attempt this would be like so: var integer: Int = 100 var decimal: Double = 12.5 integer = decimal Swift will complain if you try to do this and spit out an error on the third line: Cannot assign value of type 'Double' to type…
iOS & Swift
Swift Apprentice: Fundamentals
Apr 23 2025 · Book
This is a book for complete beginners to Apple’s modern programming language — Swift. All the code in the book works inside of Xcode’s easy-to-use playgrounds. That means you can focus on core Swift language concepts, such as classes, protocols, and generics without getting bogged down…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Strings
Apr 23 2025 · Chapter
Text processing is an essential application for any computer language, and String is Swift’s powerhouse type for text handling. Strings are bi-directional collections of Character types that balance correctness, performance and ease…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Advanced Classes
Apr 23 2025 · Chapter
This chapter continues with class types describing how Swift supports the traditional concepts of inheritance and polymorphism. You will also learn about two-phase class initialization that you will need to build proper class hierarchies. This discussion will lay the foundation for using these concepts with Swift’s value types…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Protocols
Apr 23 2025 · Chapter
…scenes since the beginning of this book. In this chapter, you’ll learn the details about protocols and see why they’re central to Swift. Introducing Protocols You define a protocol much as you do any other named type. Start with this definition for a Vehicle: protocol Vehicle { /// Return…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Introduction
Apr 23 2025 · Chapter
Welcome to the Second Edition of Swift Apprentice: Fundamentals, fully updated for Xcode 16 and Swift 6! The editions of this book were derived from the first three sections of the Swift Apprentice, Seventh Edition. This second edition continues to expand on the core concepts the are vital…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Regex
Apr 23 2025 · Chapter
Searching for patterns in text is a common task you'll encounter in your programming travels. Swift provides a power type called Regex to perform that task. Using standard syntax, you can express complicated matching patterns to extract information from text. You can use an all-new regex builder syntax…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Generics
Apr 23 2025 · Chapter
…this chapter, you’ll learn what generics are, how to write generic code, and loop back and look at the generic types in Swift - dictionaries, arrays, and optionals - from this new perspective…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Basic Control Flow
Apr 23 2025 · Chapter
…comparison operators. When you perform a comparison, such as looking for the greater of two numbers, the answer is either true or false. Swift has a data type just for this! It’s called a Bool, which is short for Boolean, after a rather clever man named George Boole…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Collection Iteration With Closures
Apr 23 2025 · Chapter
Once you have collections of items, you will want to perform operations with them. For example, sort them, filter them, add them up, etc. Swift gives you a powerful language construct, the closure, that lets you infinitely customize the behavior of such operations. In this chapter, you will learn about…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Structures
Apr 23 2025 · Chapter
…covered some fundamental building blocks of Swift. With variables, conditionals, strings, functions and collections, you’re ready to conquer the world! Well, almost. Most programs that perform complex tasks benefit from higher levels of abstraction. In addition to an Int, String or Array, most programs use new types specific…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Optionals
Apr 23 2025 · Chapter
This chapter covers optionals, a special type in Swift representing either a value or the absence of a value. By the end of this chapter, you’ll know why you need optionals and how to use them safely…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Enumerations
Apr 23 2025 · Chapter
…foot. Of course, you need a map of the terrain you’ll encounter. Since it’s the 21st century and you’re fluent in Swift, you decide to create a custom map app. As you code away, you think it would be swell to represent the cardinal directions as variables…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Functions
Apr 23 2025 · Chapter
Functions are the basic building blocks you use to structure your code in Swift. You’ll learn how to define functions to group your code into reusable units…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Arrays, Dictionaries & Sets
Apr 23 2025 · Chapter
Arrays are the most common collection type you’ll run into in Swift that keep an ordered list of elements of the same type. On the other hand, Dictionaries let you look up elements efficiently using a key. Finally, Sets maintain an unordered collection of unique elements. You’ll learn…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Conclusion
Apr 23 2025 · Chapter
…hope you learned a lot about the fundamentals of Swift in this book — and had some fun in the process! Swift is filled with language features and programming paradigms, and we hope you now feel comfortable enough with the basics of the language to move on to some more advanced…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Classes
Apr 23 2025 · Chapter
…values, you’ll generally use classes to represent objects. What does values vs. objects mean, though? Creating Classes Consider the following class definition in Swift: class Person { var firstName: String var lastName: String init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } var fullName: String { "\(firstName) \(lastName)" } } let john = Person…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Acknowledgments
Apr 23 2025 · Chapter
…many people for their assistance in making this book possible: Janie Clayton For her previous work on the first, second and third editions of Swift Apprentice. Erik Kerber For his previous work on the first and second editions of Swift Apprentice. Ben Morrow For his previous work on the first…
iOS & Swift
Chapter in Swift Apprentice: Fundamentals
Advanced Control Flow
Apr 23 2025 · Chapter
…line, you’ll learn about another loop known as the `for` loop. You’ll also learn about switch statements that are particularly powerful in Swift…