Building Generic Algorithms with Swift

Wayne Bishop
Swift Algorithms & Data Structures
4 min readDec 14, 2018

--

The introduction of Swift brings a new series of tools that make coding more friendly and expressive. Along with its simplified syntax, Swift borrows from the success of other languages to prevent common programming errors like null pointer exceptions and memory leaks.

To contrast, Objective-C has often been referred to as ‘the wild west’ of code. While extensive and powerful, many errors in Objective-C apps are discovered at runtime. This delay in error discovery is usually due to programming mistakes with memory management and typecast operations. For this essay, we’ll review a new design technique called generics and will explore how this allows code to be more useful and type-safe.

BUILDING FRAMEWORKS

As we’ll see, data structures such as linked lists, binary trees and hash tables provide a blueprint for data processing and analysis. Just like any well-designed program, data structures should also be designed for extensibility and reuse.

To illustrate, assume you’re building a simple service that lists a group of students. The data could be easily organized with a linked list-type structure and represented in the following manner:

//basic structureclass StudentNode {  var key: Student?  
var next…

--

--

Wayne Bishop
Swift Algorithms & Data Structures

I write about Swift Development & Computer Science. Get more tips on technical interview preparation at — www.waynewbishop.com