Chapters

Hide chapters

UIKit Apprentice

First Edition · iOS 14 · Swift 5.3 · Xcode 12

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

1. Introduction
Written by Matthijs Hollemans & Fahim Farook

Hi, welcome to The UIKit Apprentice: Beginning iOS Development with Swift, First Edition, the swiftest way — if you’ll pardon the pun — to iOS development mastery!

In this book, you’ll learn how to make your own iPhone and iPad apps using Apple’s Swift programming language (and Xcode 12) and the iOS UIKit framework, by building four interesting iOS apps.

The apps you’ll be making in The UIKit Apprentice
The apps you’ll be making in The UIKit Apprentice

Everybody likes games, right? So, you’ll start by building a simple but fun iPhone game named Bull’s Eye. It will teach you the basics of iPhone programming, and the other apps will build on what you learn there.

Taken together, the four apps that you’ll build cover everything you need to know to make your own apps. By the end of the book, you’ll be experienced enough to turn your ideas into real apps that you can put on the App Store!

Even if you’ve never programmed before, or if you’re new to iOS, you should be able to follow along with the step-by-step instructions and understand how these apps are made. Each chapter has a ton of illustrations to prevent you from getting lost. Not everything might make sense right away, but hang in there and all will become clear in time.

Writing your own iOS apps is a lot of fun, but it’s also hard work. If you have the imagination and perseverance, there is no limit to what you can make your apps do. It is my sincere belief that this book can turn you from a complete newbie into an accomplished iOS developer, but you do have to put in the time and effort. By writing this book, I’ve done my part. The rest is up to you…

About this book

The UIKit Apprentice will help you focus on developing applications for iOS using Apple’s UIKit framework. There are other ways to develop iOS user interfaces (UI) too — such as using SwiftUI. If you are interested in that approach, you can take a look at our other book, SwiftUI Apprentice.

This book will help you become an excellent iOS developer, but only if you let it. Here are some tips that will help you get the most out of this book.

Learn through repetition

You’re going to make several apps in this book. Even though the apps will start out quite simple, you may find the instructions hard to follow at first — especially if you’ve never done any computer programming before — because I will introduce a lot of new concepts.

It’s OK if you don’t understand everything right away, as long as you get the general idea. As you proceed through the book, you’ll go over many of these concepts again and again until they solidify in your mind.

Follow the instructions yourself

It is important that you not just read the instructions but also actually follow them. Open Xcode, type in the source code fragments, and run the app in the simulator as instructed. This helps you to see how the app gets built step by step.

Even better, play around with the code and with the Xcode settings. Feel free to modify any part of the app and see what the results are — make a small change to the code and see how it affects the entire app. Experiment and learn! Don’t worry about breaking stuff — that’s half the fun. You can always find your way back to the beginning. But better still, you might even learn something from simply breaking the code and learning how to fix it.

If you try everything but are still stuck, the forums for this book are at forums.raywenderlich.com, so drop by. I’m around most of the time and will be happy to answer any questions related to the book and any issues you might have run into.

Don’t panic — bugs happen!

You will run into problems, guaranteed. Your programs will have strange bugs that will leave you stumped. Trust me, I’ve been programming for 30 years and that still happens to me, too. We’re only humans and our brains have a limited capacity to deal with complex programming problems. In this book, I will give you tools for your mental toolbox that will allow you to find your way out of any hole you have dug for yourself.

Understanding beats copy-pasting

Too many people attempt to write iOS apps by blindly copy-pasting code that they find on blogs and other websites, without really knowing what that code does, or how it should fit into their programs.

There is nothing wrong with looking on the web for solutions — I do it all the time — but I want to give you the tools and knowledge to understand what you’re doing and why. That way, you’ll learn quicker and write better programs.

This is hands-on practical advice, not a bunch of dry theory — although we can’t avoid some theory. You are going to build real apps right from the start, and I’ll explain how everything works along the way, with lots of images that illustrate what is going on.

I will do my best to make it clear how everything fits together, why we do things a certain way and what the alternatives are.

Do the exercises

I will also ask you to do some thinking of your own — yes, there are exercises! It’s in your best interest to actually do these exercises. There is a big difference between knowing the path and walking the path… And the only way to learn programming is to do it.

I encourage you to not just do the exercises but also to play with the code you’ll be writing. Experiment, make changes, try to add new features. Software is a complex piece of machinery, and to find out how it works, you sometimes have to put some spokes in the wheels and take the whole thing apart. That’s how you learn!

Have fun!

Last but not least, remember to have fun! Step by step, you will build up your understanding of programming while making fun apps. By the end of this book, you’ll have learned the essentials of Swift and the iOS Software Development Kit (SDK). More importantly, you should have a pretty good idea of how everything goes together and how to think like a programmer.

It’s my aim that, by the time you reach the end of the book, you will have learned enough to stand on your own two feet as a developer. I am confident that, eventually, you’ll be able to write any iOS app that you want as long as you get those basics down. You still may have a lot to learn but, when you’re through with The UIKit Apprentice, you can do it without the training wheels.

Who this book is for

This book is great whether you are completely new to programming or if you come from a different programming background and are looking to learn iOS development.

If you’re a complete beginner, don’t worry — this book doesn’t assume you know anything about programming or making apps. Of course, if you do have programming experience, that helps. Swift is a new programming language but, in many ways, it’s similar to other popular languages such as PHP, C#, or JavaScript.

If you’ve tried iOS development before with the old language, Objective-C, then its low-level nature and strange syntax may have put you off. Well, there’s good news: Now that we have a modern language in Swift, iOS development has become a lot easier to pick up.

It is not my aim, with this book, to teach you all the ins and outs of iOS development. The iOS SDK is huge and there is no way we can cover everything. Fortunately, we don’t need to. You just need to master the essential building blocks of Swift and the iOS SDK. Once you understand these fundamentals, you can easily figure out how the other parts of the SDK work and learn the rest on your own terms.

The most important thing I’ll teach you is how to think like a programmer. That will help you approach any programming task, whether it’s a game, a utility, a mobile app that uses web service or anything else you can imagine.

As a programmer, you’ll often have to think your way through difficult computational problems and find creative solutions. By methodically analyzing these problems, you will be able to solve them, no matter how complex. Once you possess this valuable skill, you can program anything!

iOS 14 and later only

The code in this book is aimed exclusively at iOS version 14 and later. Each new release of iOS is such a big departure from the previous one that it just doesn’t make sense anymore to keep developing for older devices and iOS versions. Things move fast in the world of mobile computing!

The majority of iPhone, iPod touch and iPad users are pretty quick to upgrade to the latest version of iOS anyway, so you don’t need to be too worried that you’re leaving potential users behind.

Owners of older devices may be stuck with older iOS versions, but this is only a tiny portion of the market. The cost of supporting these older iOS versions for your apps is usually greater than the handful of extra customers it brings you.

It’s ultimately up to you to decide whether it’s worth making your app available to users with older devices, but my recommendation is that you focus your efforts where they matter most. Apple, as a company, relentlessly looks towards the future — if you want to play in Apple’s backyard, it’s wise to follow its lead. So back to the future it is!

What you need

It’s a lot of fun to develop for the iPhone and iPad but, like most hobbies (or businesses!), it will cost some money. Of course, once you get good at it and build an awesome app, you’ll have the potential to make that money back many times.

You will have to invest in the following:

iPhone, iPad or iPod touch. I’m assuming that you have at least one of these. iOS 14 runs on the following devices:

  • iPhone 6s and 6s Plus or newer
  • iPhone SE 1st and 2nd generation
  • iPad 5th generation or newer
  • iPad Air 3rd generation or newer
  • iPad mini 5th generation or newer
  • iPad Pro (any generation)
  • iPod touch 7th generation or newer

If you have an older device, then this is a good time to think about getting an upgrade. But don’t worry if you don’t have a suitable device: You can do most of your testing on iOS Simulator.

Note: Even though I mostly write about developing for the iPhone in this book, everything I write applies equally to the iPad and iPod touch. Aside from small hardware differences, they all use iOS (or iPadOS on the iPad) and you program them in exactly the same way. You should also be able to run the apps from this book on your iPad or iPod touch without problems.

Mac computer with an Intel processor: Any Mac that you’ve bought in the last few years will do, even a Mac mini or MacBook Air. It needs to have at least macOS Catalina 10.15.4. Xcode, the development environment for iOS apps, is a memory-hungry tool. So having at least 16 GB of RAM in your Mac would be preferable. You can get by with less, but do yourself a favor and upgrade your Mac. The more RAM, the better. A smart developer invests in good tools!

With some workarounds, it’s possible to develop iOS apps on Windows or a Linux machine, or a regular PC that has macOS installed (a “Hackintosh”), but you’ll save yourself a lot of hassle by just getting a Mac.

If you can’t afford to buy the latest model, then consider getting a second-hand Mac from eBay or some other resale vendor. Just make sure it meets the minimum requirements — Intel CPU, preferably 8 GB RAM or more. Should you happen to buy a machine that has an older version of macOS (10.12 Sierra or earlier), you can upgrade to the latest version of macOS from the online Mac App Store for free.

Apple Developer Program account: You can download all the development tools for free, and you can try out your apps on your own iPhone, iPad or iPod touch while you’re developing, so you don’t have to join the Apple Developer Program just yet. But to submit finished apps to the App Store, you will have to enroll in the paid Developer Program. This will cost you $99 per year.

See developer.apple.com/programs/ for more info.

Xcode

The first order of business is to download and install Xcode and the iOS SDK.

Xcode is the development tool for iOS apps. It has a text editor where you’ll type in your source code, and it has a visual editor for designing your app’s user interface.

Xcode transforms the source code that you write into an executable app and launches it in the iOS Simulator or on your iPhone/iPad. Because no app is bug-free, Xcode also has a debugger that helps you find defects in your code. Unfortunately, it won’t automatically fix them for you; that’s still something you have to do yourself.

You can download Xcode for free from the Mac App Store (apple.co/2wzi1L9). This requires at least an up-to-date version of macOS Catalina (10.15.4). So, if you’re still running an older version of macOS, you’ll first have to upgrade to the latest version of macOS — also available for free from the Mac App Store. Get ready for a big download, as the full Xcode package is over 10 GB!

Important: You may already have a version of Xcode on your system that came pre-installed with your version of macOS. That version could be hopelessly outdated, so don’t use it. Apple puts out new releases of Xcode on a regular basis and you are encouraged to always develop with the latest Xcode and the latest available SDK on the latest version of macOS.

This revision of the book was written using Xcode version 12 and the iOS 14 SDK on macOS Catalina (10.15.5). By the time you’re reading this, the version numbers might have gone up again. We will do our best to keep the PDF versions of the book up-to-date with new releases of the development tools and iOS versions, but don’t panic if the screenshots don’t correspond 100% to what you see on your screen. In most cases, the differences will be minor.

Many older books and blog posts (anything before 2014) talk about iOS development in Xcode 5 or earlier. Ignore them; they’re from an earlier era of iOS development and use a completely different programming language. If you’re look at an article or book that predates Swift, it’s seriously out of date.

More recent materials may mention Xcode versions 6 all the way to 11, which, at first glance, are similar to Xcode 12, but may differ in some of the details.

So if you’re reading an article and you see a picture of Xcode that looks different from yours, the author might be writing about an older version. You may still be able to get something out of those articles, as the programming examples are still valid. It’s just Xcode that is slightly different.

What’s ahead: an overview

The UIKit Apprentice is spread across four apps, moving from beginning to intermediate topics. For each app, you will build it from start to finish, from scratch! Let’s take a look at what’s ahead.

App 1: Bull’s Eye

You’ll start by building a game called Bull’s Eye. You’ll learn how to use Xcode, Interface Builder, and Swift in an easy-to-understand manner.

App 2: Checklists

For your next app, you’ll create your own to-do list app. You’ll learn about the fundamental design patterns that all iOS apps use, and about table views, navigation controllers and delegates. Now you’re making apps for real!

App 3: MyLocations

For your third app, you’ll develop a location-aware app that lets you keep a list of spots that you find interesting. In the process, you’ll learn about Core Location, Core Data, Map Kit, and much more!

App 4: StoreSearch

Mobile apps often need to talk to web services and that’s what you’ll do in your final app. You’ll make a stylish app that lets you search for products on the iTunes store using HTTP requests and JSON.

Let’s get started and turn you into a full-fledged iOS developer!

The language of the computer

The iPhone may pretend that it’s a phone, but it’s really a pretty advanced computer that also happens to have the ability to make phone calls.

Like any computer, the iPhone works with ones and zeros. When you write software to run on iPhone, you somehow have to translate the ideas in your head into those ones and zeros that the computer can understand.

Fortunately, you don’t have to write any ones and zeros yourself. That would be a bit too much to ask of the human brain. On the other hand, everyday English is not precise enough to use for programming computers.

So you will use an intermediary language, Swift, that is a little bit like English, so it’s reasonably straightforward for us humans to understand; at the same time, it can be easily translated into something the computer can understand as well.

This is an approximation of the language that the computer speaks:

Actually, what the computer sees is this:

The movl and calll instructions are just there to make things more readable for humans. I don’t know about you, but for me it’s still hard to make much sense out of it.

It certainly is possible to write programs in that arcane language — that is what people used to do in the old days when computers cost a few million bucks apiece and took up a whole room — but I’d rather write programs that look like this:

func handleMusicEvent(command: Int, noteNumber: Int, velocity: Int) {

  if command == NoteOn && velocity != 0 {
    playNote(noteNumber + transpose, velocityCurve[velocity] / 127)

  } else if command == NoteOff || 
           (command == NoteOn && velocity == 0) {
    stopNote(noteNumber + transpose, velocityCurve[velocity] / 127)

  } else if command == ControlChange {
    if noteNumber == 64 {
      damperPedal(velocity)
    }
  }
}

The above code snippet is from a sound synthesizer program. It looks like something that almost makes sense. Even if you’ve never programmed before, you can sort of figure out what’s going on. It’s almost English.

Swift is a hot new language that combines traditional object-oriented programming with aspects of functional programming. Fortunately, Swift has many things in common with other popular programming languages, so if you’re already familiar with C#, Python, Ruby or JavaScript, you’ll feel right at home with Swift.

Swift is not the only option for making apps. Until just a few years ago, iOS apps were programmed in Objective-C, which is an object-oriented extension of the tried-and-true C language. Because of its heritage, Objective-C has some rough edges and is not really up to the demands of modern developers. That’s why Apple created a new language.

Objective-C will still be around for a while, but it’s obvious that the future of iOS development is Swift. All the cool kids are using it already.

C++ is another language that adds object-oriented programming to C. It is very powerful, but as a beginning programmer, you probably want to stay away from it. I only mention it because C++ can also be used to write iOS apps, and there is an unholy marriage of C++ and Objective-C named Objective-C++ that you may come across from time to time.

I could have started The UIKit Apprentice with an in-depth exploration of the features of Swift, but you’d probably fall asleep halfway. So, instead, I will explain the language as we go along — very briefly at first, but more in-depth later.

In the beginning, the general concepts — what is a variable, what is an object, how do you call a method, and so on — are more important than the details. Slowly but surely, all the arcane secrets of the Swift language will be revealed to you!

Are you ready to begin writing your first iOS app?

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.