Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fourth Edition · iOS 15 · Swift 5.5 · Xcode 13

“As an iOS developer, should I learn about algorithms and data structures?” This question comes up a lot in the online discussion groups that I hang out in. Let me attempt to answer that question by sharing some of my own experiences as a professional developer.

Like many people who got into programming as a hobby, I am mostly self-taught and don’t have a formal computer science background. Over the years, I may have picked up the occasional tidbit of knowledge, but I never had a solid foundation in the theory behind algorithms and data structures.

Was this a problem? Nope, most of the time I was just fine. As modern day developers, using the data structures provided by the standard library is more than enough for much of the software we write. Swift’s arrays and dictionaries go a long way.

However… there have been times when I was not able to write the software I wanted to, either because I did not understand how to approach the problem or because I did not understand how to make a solution that was fast enough.

Whenever I ran into such a programming roadblock, I felt that maybe I wasn’t smart enough to be a software developer after all — but really, I just lacked the tools needed to solve these problems. With a better vocabulary of algorithms and data structures, I would have been able to write all the software I wanted.

Usually, my first attempt to solve an unknown problem was to use an “obvious” brute-force algorithm that tried all possible combinations. Sometimes that works well enough, but more often than not, a naïve solution like this takes days to compute and is just not feasible. (I had never heard of Big-O notation, either!)

But in most cases, there’s no need to derive a solution by yourself from first principles! Often, these problems have already been solved many times before by other people, and they have well-known solutions. The trick is to understand what sort of problem you’re dealing with — and that’s where learning about algorithms and data structures pays off. Once you know what a problem is called, it’s easy to find a solution for it.

So then, do you need to learn about algorithms and data structures as an iOS developer? I’d say no; you can probably solve 95% of all your programming needs without them. But at some point, you’re going to get stuck on a problem where the naïve brute-force solution is not feasible. In situations like that, it’s good to have an algorithms and data structures toolbox.

A few years ago, I wanted to fill this gap in my knowledge. I bought a number of algorithms books and started implementing the algorithms in Swift. Because I believe that teaching is the best way to learn, I decided to create a repo on GitHub with my own descriptions of the things I was learning about. And so the Swift Algorithm Club was born. It started receiving contributions from others almost right away, and together we’ve built a cool community of algorithms enthusiasts with descriptions for 100 algorithms and data structures… and counting.

Because handling the number of pull requests became too much for me to handle as a side project, I donated the code to raywenderlich.com and it found a great home there. The new maintainers of the repo, Kelvin and Vincent, have done a great job of making it the awesome resource that it is today — and now they’ve taken that knowledge and made it even better by turning it into this book!

If you’re interested in leveling up on your algorithms and data structures, you’ve come to the right place. And when you’re done reading this book, head on over to The Swift Algorithm Club on GitHub for even more A&DS goodness!

—Matthijs Hollemans

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.