Skip to main content

About this book

Learn how to code for the iMac, Mac mini, Mac Pro, and MacBook using Swift, Apple's hottest programming language.
Fully updated to cover the new MacBook Touch Bar, macOS Programming for Absolute Beginners will not only teach complete programming novices how to write macOS programs, but it can also help experienced programmers moving to the Mac for the first time. You will learn the principles of programming, how to use Swift and Xcode, and how to combine your knowledge into writing macOS programs.
If you've always wanted to learn coding but felt stymied by the limitation of simplistic programming languages or intimidated by professional but complicated programming languages, then you'll want to learn Swift. Swift is your gateway to both Mac and iOS app development while being powerful and easy to learn at the same time, and macOS Programming for Absolute Beginners is the perfect place to start - add it to your library today.What You'll Learn
Master the basic principles of object-oriented programming
Use Xcode, the main programming tool used for both macOS and iOS development
See what makes Swift unique and powerful as a programming language and why you should learn it
Create macOS programs using Swift and Xcode
Apply interface principles that follow Apple's Human Interface Guidelines
Take advantage of the new Touch Bar

Who This Book Is For
People who want to learn programming for the first time and for experienced programmers wanting to learn Xcode and the Mac for the first time.

Table of Contents

Chapter 1. Understanding Programming

Programming is nothing more than writing step-by-step instructions for a computer to follow. If you’ve ever written down the steps for a recipe or scribbled directions for taking care of your pets while you're on vacation, you’ve already gone through the basic steps of writing a program. The key is simply knowing what you want to accomplish and then making sure you write the correct instructions that will tell someone how to achieve that goal.
Wallace Wang

Chapter 2. Getting to Know Xcode 8

To write programs in Swift for the Macintosh, you need to use Xcode. Apple developed Xcode as a professional programming tool that they give away free to encourage everyone to write software for macOS and their other operating systems such as iOS, tvOS, and watchOS. Despite being a free program, Xcode is a powerful program used by major companies including Microsoft, Adobe, Google, and even Apple. With Xcode on a Macintosh, you have one of the most powerful programming tools for creating macOS programs and iOS, tvOS, and watchOS apps.
Wallace Wang

Chapter 3. The Basics of Creating a Mac Program

Whatever type of macOS program you want to create, such as a video game or a custom program for dentists or real estate agents, you'll always go through the same basic steps. First, you’ll need to create a macOS project. This creates a bare-bones macOS program that includes a generic user interface.
Wallace Wang

Chapter 4. Getting Help

The best way to learn any new skill is to have someone show you what you need to learn. Since that's not always possible, you'll be happy to know that Xcode comes with plenty of built-in help features to make using Xcode less stressful and more enjoyable.
Wallace Wang

Chapter 5. Learning Swift with Playgrounds

To write any program, you need to choose a programming language. A programming language lets you define commands for the computer to follow. There's no one "best" programming language because every programming language is meant to solve a specific problem. This means that a certain programming language may be great at solving certain types of problems, but horrible at solving other types of programs.
Wallace Wang

Chapter 6. Manipulating Numbers and Strings

Every program needs to store data temporarily in variables. However, for a program to be useful, it must also manipulate that data somehow to calculate a useful result. A spreadsheet lets you add, subtract, multiply, and divide numbers. A word processor manipulates text to correct your spelling and format text. Even a video game responds to joystick movements to calculate a new position for the player's object such as a cartoon person, airplane, or car. Using data to calculate a new result is the whole purpose of every program.
Wallace Wang

Chapter 7. Making Decisions with Branches

After a program receives data, it needs to manipulate that data somehow to return a useful result. Simple programs manipulate data the same way, but more complicated programs need to make decisions on how to manipulate that data.
Wallace Wang

Chapter 8. Repeating Code with Loops

The basic goal of programming is to write as little code as possible that does as much as possible. The less code you write, the easier the program will be to understand and to modify later. The more your code does, the more powerful your program will be.
Wallace Wang

Chapter 9. Arrays and Dictionaries

Almost every program needs to accept data so it can manipulate that data and calculate a useful result. The simplest way to store data temporarily is through variables that can store numbers or text strings. However, what if you need to store multiple chunks of data such as a list of names or a list of product numbers?
Wallace Wang

Chapter 10. Tuples and Sets

Variables are good for storing individual chunks of data, and arrays and dictionaries are good for storing lists of related data. For greater flexibility, Swift also offers additional data structures called tuples and sets.
Wallace Wang

Chapter 11. Storing Code in Functions

Every program stores data (in variables or in data structures such as arrays, sets, or dictionaries) and then manipulates that data somehow. Code that manipulates data is called an algorithm.
Wallace Wang

Chapter 12. Defining Custom Data Types

To store data, Swift provides common data types such as integers (Int), decimal numbers (Float or Double), and text (String). In addition, Swift also offers Boolean data types (Bool) for holding true or false values.
Wallace Wang

Chapter 13. Creating Classes and Objects

The main idea is to divide a large program into separate objects where each object ideally represents a physical entity. For example, if you are creating a program to control a car, one object may represent the car's engine, a second object may represent the car's entertainment system, and a third object may represent the car's heating and cooling system.
Wallace Wang

Chapter 14. Inheritance, Polymorphism, and Extending Classes

To protect its data and methods from other parts of a program, an object isolates or encapsulates its code. Encapsulation is one prime advantage of object-oriented programming because it creates self-contained code that you can easily modify or replace without affecting any other part of a program.
Wallace Wang

Chapter 15. Creating a User Interface

While it's possible to create programs that never interact with a user at all (such as a program that controls a traffic light), it's far more common to create programs that display a user interface of some kind. Typically, this means showing a window filled with different items such as labels to display text, text fields to allow the user to type something in, and buttons or pull-down menus to give the user control over the program.
Wallace Wang

Chapter 16. Working with Storyboards

The most common part of every program’s user interface is a window that displays items such as buttons and text fields. In Xcode, windows are called views. In all but the simplest programs, a user interface will likely consist of two or more windows or views. This means your program needs to know how to open additional windows and close them again.
Wallace Wang

Chapter 17. Choosing Commands with Buttons

Every program needs to give the user a way to control the computer. In the old days, that meant knowing how to type the proper commands to make a program work, but with today's graphical user interfaces, the easier way to control a program is to choose from a list of available commands. The simplest way to give a command to a program is through a button.
Wallace Wang

Chapter 18. Making Choices with Radio Buttons, Check Boxes, Date Pickers, and Sliders

Rather than let the user choose a specific command through a button, user interfaces often give choices to pick from. Such choices let the user pick one or more options, such as customizing the way a program works. When a user interface needs to offer multiple choices, the two most common ways to offer options are through radio buttons and check boxes.
Wallace Wang

Chapter 19. Using Text with Labels, Text Fields, and Combo Boxes

When a program needs to offer a limited range of valid options to the user, that's when you want to use check boxes, radio buttons, date pickers, or sliders. However, sometimes a program needs to allow the user to type in data that can never be predicted ahead of time, such as a person's name. When a program needs to allow the user to type in data, that's when you need to use a text field or a combo box.
Wallace Wang

Chapter 20. Designing a User Interface with Constraints

A user interface lets people control a program, give a program data, and receive data back again. Since people can only interact with a program through its user interface, it’s crucial that the user interface is uncluttered and easy to understand.
Wallace Wang

Chapter 21. Using Alerts and Panels

In every program, you can design the unique features of your program while letting the Cocoa framework worry about making your program look and behave like a standard macOS program. To create the common features of nearly every macOS program, you can use alerts and panels.
Wallace Wang

Chapter 22. Creating Pull-Down Menus

While your program may display buttons to represent commands, too many buttons can clutter the screen. To avoid trying to cram multiple buttons on the screen, you can group related commands in multiple pull-down menus.
Wallace Wang

Chapter 23. Programming the Touch Bar

The Touch Bar displays contextual shortcuts depending on what you’re doing at the time. For example, if you’re typing, the Touch Bar might display suggested words that you can tap so you don’t have to type the entire word out. If you’re editing a video, the Touch Bar lets you scroll back and forth through the video.
Wallace Wang

Chapter 24. Protocol-Oriented Programming

When Apple introduced Swift during its Worldwide Developer's conference in 2014, it was promoted as an easier, safer, and faster programming language for iOS, macOS, tvOS, and watchOS development than Objective-C. While both Swift and Objective-C allow object-oriented programming, Swift goes one step further and offers protocol-oriented programming as well.
Wallace Wang

Chapter 25. Defensive Programming

Programmers tend to be optimistic, because when they write code, they assume that it will work correctly. However, it's often better to be more pessimistic when it comes to programming. Instead of assuming your code will work the first time, it's safer to assume your code won't work at all. This forces you to be extra careful when writing Swift code to make sure it does exactly what you want and doesn't do anything unexpected.
Wallace Wang

Chapter 26. Simplifying User Interface Design

Designing a user interface can be challenging. Not only do you need to design a user interface that's easy to use, but you also need to design an adaptive user interface that can respond to any changes the user might make to a window's size. If the user shrinks a window, your user interface must shrink accordingly without cutting any items off. If the user enlarges a window, your user interface must expand accordingly to maintain a consistent appearance.
Wallace Wang

Chapter 27. Debugging Your Programs

In the professional world of software development, you'll actually spend more time modifying existing programs than you ever will creating new ones. However, whether writing new programs or editing existing ones, it doesn't matter how much experience or education you might have because even the best programmers can make mistakes. In fact, you can expect that you will make mistakes no matter how careful you may be. Once you accept this inevitable fact of programming, you can learn how to find and fix your mistakes.
Wallace Wang

Chapter 28. Planning a Program Before and After Coding

Before you invest days, weeks, months, or years working on a program, make sure the world even wants your program in the first place. If you're writing a program for yourself, then you can ignore what the rest of the world thinks. However, if you plan on selling your programs to others, make sure your program has a future before you even begin.
Wallace Wang
Additional information