ShapeShapeGrouppointGroupchapter-listShapeShape Copy 12ShapeShape Copy 3ShapeGroupShapedropdowneditoreditorCombined ShapegridShape Copy 4Shape Copy 7ShapelanguagelanguageShapemegaphonepaperbackShapeplatformnoun_419259-2 copyplay-iconBS-Question-Rounded-Circlequotesnoun_610406stacked-books-1stacked-books-2Shape Copy 5 Combined Shape Group Group Group Group Group Group
The Advanced Swift Spring Bundle is only available until Friday, April 27!

Advanced Swift Spring Bundle

$99.99 $164.97
  • Platform iOS 11
  • Language Swift 4
  • Editor Xcode 9.3
3-Book Bundle ($99.99) $164.97 40% off!

Includes our three newest, full-length digital edition books:

  • Data Structures and Algorithms in Swift
  • Design Patterns by Tutorials
  • Realm: Building Modern Swift Apps with Realm Database

Note: Data Structures and Algorithms in Swift and Design Patterns by Tutorials are available under an Early Access Release model. You’ll receive free updates to these two books as further chapters become available in the very near future.

Sign in to Buy → *Includes ePub, PDF and Project Files
Developer Guide

Advanced

For Intermediate and Advanced iOS Developers

Level Up Your Swift Development Skills!

If you’re comfortable with Swift, and want to take your development skills to the next level, then this is the bundle for you.

Data Structures and Algorithms in Swift teaches you how to implement the most common and useful data structures and algorithms in Swift. Use it to ace your next whiteboard interview, or dig deep into the theory of data structures and find ways to make your code more efficient.

Design Patterns by Tutorials brings the theoretical benefits of common design patterns directly to your Swift apps. Learn the “why” behind many common design patterns, and then take that theory and use it to solve design issues in the real-world apps included with the book.

Realm: Building Modern Swift Apps with Realm Database starts off by showing you to create a database with a single line of code, and builds on that simplicity to design and implement complex object schemas in the multiple Realm projects included with this book.

Note: Data Structures and Algorithms in Swift and Design Patterns by Tutorials are available under an Early Access Release model. You’ll receive free updates to these two books as further chapters become available in the very near future. Realm: Building Modern Swift Apps with Realm Database is available as a complete book.

What's included

Data Structures and Algorithms in Swift

1.

Preface

Data structures are a well-studied area, and the concepts are language agnostic; a data structure from C is functionally and conceptually identical to the same data structure in any other language, such as Swift. At the same time, the high-level expressiveness of Swift make it an ideal choice for learning these core concepts without sacrificing too much performance.

2.

Swift Standard Library

Before you dive into the rest of this book, you’ll first look at a few data structures that are baked into the Swift language. The Swift standard library refers to the framework that defines the core components of the Swift language. Inside, you’ll find a variety of tools and types to help build your Swift apps.

3.

Linked List

A linked list is a collection of values arranged in a linear unidirectional sequence. A linked list has several theoretical advantages over contiguous storage options such as the Swift Array, including constant time insertion and removal from the front of the list, and other reliable performance characteristics.

4.

Stack Data Structure

The stack data structure is identical in concept to a physical stack of objects. When you add an item to a stack, you place it on top of the stack. When you remove an item from a stack, you always remove the topmost item. Stacks are useful, and also exceedingly simple. The main goal of building a stack is to enforce how you access your data.

5.

Queues

Lines are everywhere, whether you are lining up to buy tickets to your favorite movie, or waiting for a printer machine to print out your documents. These real-life scenarios mimic the queue data structure. Queues use first-in-first-out ordering, meaning the first element that was enqueued will be the first to get dequeued. Queues are handy when you need to maintain the order of your elements to process later.

6.

Trees

The tree is a data structure of profound importance. It is used to tackle many recurring challenges in software development, such as representing hierarchical relationships, managing sorted data, and facilitating fast lookup operations. There are many types of trees, and they come in various shapes and sizes.

7.

Binary Trees

In the previous chapter, you looked at a basic tree where each node can have many children. A binary tree is a tree where each node has at most two children, often referred to as the left and right children. Binary trees serve as the basis for many tree structures and algorithms. In this chapter, you’ll build a binary tree and learn about the three most important tree traversal algorithms.

8.

Binary Search Trees

A binary search tree facilitates fast lookup, addition, and removal operations. Each operation has an average time complexity of O(log n), which is considerably faster than linear data structures such as arrays and linked lists.

9.

AVL Trees

In the previous chapter, you learned about the O(log n) performance characteristics of the binary search tree. However, you also learned that unbalanced trees can deteriorate the performance of the tree, all the way down to O(n). In 1962, Georgy Adelson-Velsky and Evgenii Landis came up with the first self-balancing binary search tree: the AVL Tree.

10.

Tries

The trie (pronounced as “try”) is a tree that specializes in storing data that can be represented as a collection, such as English words. The benefits of a trie are best illustrated by looking at it in the context of prefix matching, which is what you’ll do in this chapter.

11.

Binary Search

Binary search is one of the most efficient searching algorithms with a time complexity of O(log n). This is comparable with searching for an element inside a balanced binary search tree. To perform a binary search, the collection must be able to perform index manipulation in constant time, and must be sorted.

12.

The Heap Data Structure

A heap is a complete binary tree, also known as a binary heap, that can be constructed using an array. Heaps come in two flavors: Max heaps and Min heaps. Have you seen the movie Toy Story, with the claw machine and the squeaky little green aliens? Imagine that the claw machine is operating on your heap structure, and will always pick the minimum or maximum value, depending on the flavor of heap.

13.

Priority Queue

Queues are simply lists that maintain the order of elements using first-in-first-out (FIFO) ordering. A priority queue is another version of a queue that, instead of using FIFO ordering, dequeues elements in priority order. A priority queue is especially useful when you need to identify the maximum or minimum value given a list of elements.

14.

O(n²) Sorting Algorithms

O(n²) time complexity is not great performance, but the sorting algorithms in this category are easy to understand and useful in some scenarios. These algorithms are space efficient; they only require constant O(1) additional memory space. In this chapter, you'll be looking at the bubble sort, selection sort, and insertion sort algorithms.

15.

Merge Sort

Merge sort is one of the most efficient sorting algorithms. With a time complexity of O(log n), it’s one of the fastest of all general-purpose sorting algorithms. The idea behind merge sort is divide and conquer: to break up a big problem into several smaller, easier to solve problems and then combine those solutions into a final result. The merge sort mantra is to split first, and merge after.

16.

Radix Sort

In this chapter, you’ll look at a completely different model of sorting. So far, you’ve been relying on comparisons to determine the sorting order. Radix sort is a non-comparative algorithm for sorting integers in linear time. There are multiple implementations of radix sort that focus on different problems. To keep things simple, in this chapter you’ll focus on sorting base 10 integers while investigating the least significant digit (LSD) variant of radix sort.

17.

Heap Sort

Heapsort is another comparison-based algorithm that sorts an array in ascending order using a heap. This chapter builds on the heap concepts presented in Chapter 12, “The Heap Data Structure”. Heapsort takes advantage of a heap being, by definition, a partially sorted binary tree.

18.

Quicksort

In the preceding chapters, you’ve learned to sort an array using comparison-based sorting algorithms, merge sort, and heap sort. Quicksort is another comparison-based sorting algorithm. Much like merge sort, it uses the same strategy of divide and conquer. In this chapter, you will implement Quicksort and look at various partitioning strategies to get the most out of this sorting algorithm.

19.

Graphs

What do social networks have in common with booking cheap flights around the world? You can represent both of these real-world models as graphs! A graph is a data structure that captures relationships between objects. It is made up of vertices connected by edges. In a weighted graph, every edge has a weight associated with it that represents the cost of using this edge. This lets you choose the cheapest or shortest path between two vertices.

20.

Breadth First Search

In the previous chapter, you explored how graphs can be used to capture relationships between objects. Several algorithms exist to traverse or search through a graph's vertices. One such algorithm is the breadth-first search algorithm, which can be used to solve a wide variety of problems, including generating a minimum spanning tree, finding potential paths between vertices, and finding the shortest path between two vertices.

21.

Depth-First Search

In the previous chapter, you looked at breadth-first search where you had to explore every neighbor of a vertex before going to the next level. In this chapter, you will look at depth-first search, which has applications for topological sorting, detecting cycles, path finding in maze puzzles, and finding connected components in a sparse graph.

22.

Dijkstra’s Algorithm

Have you ever used the Google or Apple Maps app to find the shortest or fastest from one place to another? Dijkstra’s algorithm is particularly useful in GPS networks to help find the shortest path between two places. Dijkstra’s algorithm is a greedy algorithm, which constructs a solution step-by-step, and picks the most optimal path at every step.

23.

Prim’s Algorithm

In previous chapters, you’ve looked at depth-first and breadth-first search algorithms. These algorithms form spanning trees. In this chapter, you will look at Prim’s algorithm, a greedy algorithm used to construct a minimum spanning tree. A minimum spanning tree is a spanning tree with weighted edges where the total weight of all edges is minimized. You’ll learn how to implement a greedy algorithm to construct a solution step-by-step, and pick the most optimal path at every step.

Design Patterns by Tutorials

1.

What are Design Patterns?

Design patterns aren’t concrete implementations, but rather, serve as starting points for writing code. They describe generic solutions to problems that many experienced developers have encountered many times before. What does this mean exactly...? Learn this and more in this chapter.

2.

How to Read a Class Diagram

ou may have heard of Unified Modeling Language, which is a standard language for creating class diagrams, architectural drawings and other system illustrations. A complete discussion of UML is beyond the scope of this book, but you won't need to understand a lot of UML in your day-to-day iOS development. Instead, you'll learn a subset of UML in this chapter that's useful for creating class diagrams and describing design patterns.

3.

Model-View-Controller Pattern

The model-view-controller (MVC) pattern separates objects into three distinct types: models, views and controllers! MVC is very common in iOS programming, because it's the design pattern that Apple chose to adopt heavily in UIKit. In this chapter you’ll implement the MVC pattern as you start to build out the first app in this book: RabbleWabble.

4.

Delegation Pattern

The delegation pattern enables an object to use another “helper” object to provide data or perform a task rather than the task itself. By relying on a delegate protocol instead of a concrete object, the implementation is much more flexible: any object that implements the protocol can be used as the delegate! You'll continue the RabbleWabble app from the previous chapter, and add a menu controller to select the group of questions.

5.

Strategy Pattern

The strategy pattern defines a family of interchangeable objects that can be set or switched at runtime: the object using a strategy, the strategy protocol, and the set of strategies. You continue to build out Rabble Wabble and learn how these three components work together in the strategy pattern.

6.

Singleton Pattern

The singleton pattern restricts a class to only one instance. Every reference to the class refers to the same underlying instance. It is extremely common in iOS app development, because Apple makes extensive use of it.

7.

Memento Pattern

The memento pattern allows an object to be saved and restored. you can use this pattern to implement a save game system, where the originator is the game state (such as level, health, number of lives, etc), the memento is saved data, and the caretaker is the gaming system. You can also persist an array of mementos, representing a stack of previous states. You can use this to implement features such as undo/redo stacks in IDEs or graphics software.

8.

Observer Pattern

The observer pattern lets one object observe changes on another object. You’ll learn two different ways to implement the observer pattern in this chapter: using key value observation (KVO), and using an `Observable` wrapper.

9.

Builder Pattern

The builder pattern allows the creation of complex objects step-by-step, instead of all at once, via an initializer. For example, you can use this pattern to implement a “hamburger builder”. The product could be a“`hamburger” model, which has inputs such as meat selection, toppings and sauces. The director could be an “employee” object, which knows how to build hamburgers, or it could be a view controller that accepts inputs from the user.

10.

Model-View-ViewModel Pattern

Use this pattern when you need to transform models into another representation for a view. This pattern compliments MVC especially well. You’ll embark on a new project — CoffeeQuest — to help you find the best coffee shops around.

11.

Factory Pattern

The factory pattern provides a way to create objects without exposing creation logic. Technically, there are multiple "flavors" of this pattern, including a simple factory, abstract factory and others. However, each of these share a common goal: to isolate object creation logic within its own construct.

12.

Adapter Pattern

Classes, modules, and functions can’t always be modified, especially if they’re from a third-party library. Sometimes you have to adapt instead! You can create an adapter either by extending an existing class, or creating a new adapter class. This chapter will show you how to do both.

13.

Iterator Pattern

The Iterator Pattern provides a standard way to loop through a collection. Use the iterator pattern when you have a class or struct that holds a group of ordered objects, and you want to make it iterable using a “for in” loop.

14.

Prototype Pattern

Methods are merely functions that reside in a class. In this chapter, you’ll take a closer look at methods and see how to add methods onto classes that were created by someone else. You’ll also start in on a new project with this chapter: a drawing app named MirrorPad.

15.

State Pattern

The state pattern is a behavioral pattern that allows an object to change its behavior at runtime. It does so by changing its current state. "State" here means the set of data that describes how a given object should behave at a given time.

16.

Multicast Delegate Pattern

The multicast delegate pattern is a behavioral pattern that’s a variation on the delegate pattern. It allows you to create one-to-many delegate relationships, instead of one-to-one relationships in a simple delegate. Use this pattern to create one-to-many delegate relationships.

17.

Facade Pattern

The facade pattern is a structural pattern that provides a simple interface to a complex system. Use this pattern whenever you have a system made up of multiple components and want to provide a simple way for users to perform complex tasks.

18.

Flyweight Pattern - Coming Soon

This chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

19.

Mediator Pattern - Coming Soon

This chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

20.

Composite Pattern - Coming Soon

This chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

21.

Command Pattern - Coming Soon

KThis chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

22.

Chain of Responsibility Pattern - Coming Soon

This chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

23.

Decorator Pattern - Coming Soon

This chapter is currently under development. Stay tuned for this chapter in an updated release in the near future!

Realm: Building Modern Swift Apps with Realm Database

1.

Hello Realm!

Realm finds the sweet spot between the simplicity of storing data as JSON on disk and using heavy, slow ORMs like Core Data or similar that are built on top of SQLite. In this book you are going to learn plenty about Realm, how to build iOS apps with Realm, and you’ll pick up some tips and tricks along the way about getting the most out of the platform.

2.

Your First Realm App

In the previous chapter, you learned about the Realm Database, how it works and what problems it can solve for you as a developer. Now you'll take a leap of faith and dive right into creating an iOS app that uses Realm to persist data on disk while following this tutorial-style chapter.

3.

Object Basics and Data Types

You now have a grasp of the easy and clean way of writing Realm-related code. In this chapter, you’ll dive deeper into Realm’s Swift API and go over many of the available classes and their methods in order to get a solid understanding of Realm’s superpowers, as well as some of its limitations.

4.

Schema Relationships

You’ve mastered your object-type properties and your primitives, learned how to add a primary key and indices, and other important details. Your data, however, isn’t isolated when in a Realm. You can have many different objects connected to each other in all sorts of useful and meaningful ways. This chapter will teach you all about building powerful and efficient relationships between objects.

5.

Reading and Writing Objects

You’ve had a sneak peek into persisting objects and reading them back, but you barely scratched the surface of dealing with stored objects, as you’ll realize in this chapter. In the first half of this chapter you’ll look into fetching, querying, and sorting persisted data from Realm. In the second half, you will cover the Realm APIs that let you add, update, and delete objects in your Realm.

6.

Notifications and Reactive Apps

You are probably eager to use the in-depth knowledge you soaked up working through the last few chapters in practice, and rightfully so. In this chapter, you’ll learn about Realm’s built-in notification APIs. Realm features a rather clever system of detecting any changes, regardless of the thread or process responsible for those changes, and deliver notifications to any observers.

7.

Realm Configurations

In this chapter that covers Realm configurations, you’ll take a step back (or is it one forward?), dig into working with the Realm class itself, and learn about how to configure Realm using Realm.Configuration. You’ll learn how to work with different Realm files on disk and in-memory, as well as how to use advanced features such as data encryption.

8.

Multiple Realms / Shared Realms

In this chapter, you’re going to make use of your existing Realm skills while learning some new ones. You’re going to use multiple configurations, read and write data, use notifications to build reactive UI, and explore new topics like sharing data between your app and a Today extension.

9.

Dependency Injection and Testing

In this chapter, you’re going to touch on two important topics: how to use dependency injection to improve the architecture of your Flash Cards apps, and how to write both synchronous and asynchronous tests powered by Realm. This chapter won’t delve into topics such as test driven development, but will instead focus specifically on tips and tricks for testing classes that use Realm objects and depend on Realm-specific functionality such as change notifications.

10.

Effective Multi-threading

Many of the issues you would experience with database ORMs and/or other databases are rooted in asynchronous, multi-threaded code. Accessing your data from concurrent threads in an efficient and safe manner is, unsurprisingly, not very straightforward and quite error-prone. Realm saves you from the hassles of over-thinking concurrent threads, since it’s been planned with a deeply integrated multi-threading strategy which makes concurrent access to the database a walk in the park.

11.

Beginning Realm Migrations

Nothing in life (and code) remains static forever. So what happens, then, when the app you’re working on becomes wildly successful and you need release a new version, and then another one, and another one? In this chapter you’ll learn how you can migrate the schema of a Realm file as it evolves alongside your app.

12.

Advanced Schema Migrations

In this chapter, you’re going to build a more complex migration in an Xcode project. The demo is designed in such a way that you can build three different versions of the same app. This will allow you to write code and simulate real-life app upgrades, all from the comfort of a single Xcode project.

14.

Real-Time Sync with Realm Cloud

In this chapter, you’re going to build a more complex migration in an Xcode project. The demo is designed in such a way that you can build three different versions of the same app. This will allow you to write code and simulate real-life app upgrades, all from the comfort of a single Xcode project.

Meet the authors

Kelvin Lau iOS Team
Vincent Ngo iOS Team
Joshua Greene iOS Team Team
Jay Strawn iOS Team
Marin Todorov iOS Team

Get the Advanced Swift Spring Bundle

3-Book Bundle ($99.99) $164.97 40% off! Includes our three newest, full-length digital edition books:

  • Data Structures and Algorithms in Swift
  • Design Patterns by Tutorials
  • Realm: Building Modern Swift Apps with Realm Database
Top