Chapters

Hide chapters

macOS Apprentice

Third Edition · macOS 26 · Swift 6 · Xcode 26

Section II: Building With SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

Section III: Building With AppKit

Section 3: 6 chapters
Show chapters Hide chapters

13. Powering Up Your Table
Written by Sarah Reichelt

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you designed and built the interface for users to select a movie and read its details.

Now, you’ll move on to adding more features to the table itself. You’ll create a toolbar with a search field and you’ll make the table columns sortable.

You’ve already given users the ability to mark their favorite movies, but this data doesn’t persist between app launches. In order to store the favorites settings, you’ll learn how to save the data and reload it. This involves interacting with the Mac sandbox.

Adding a Search Field

With over 34,000 movies in the table, scrolling to find the one you want isn’t easy. So it’s time to add search. The appropriate place for a search field is in the window’s toolbar, and that’s where you’ll add it.

Open Xcode with your project from the previous chapter or use the starter project from the downloads for this chapter.

Open Main.storyboard to set up the toolbar. You might expect to add it to ViewController since that’s where you’ll use it, but a toolbar is part of a window, so you add it to the window.

Scroll the display so you can see the window with the Movies title. Press Shift-Command-L to open the Library and search for toolbar, then drag a Toolbar into the window:

The default toolbar.
The default toolbar.

By default, the toolbar has three items (apart from spacers) and you don’t want any of them. Double-click anywhere in the toolbar to open its editor:

The toolbar editor
The toolbar editor

One at a time, select Colors, Fonts and Print from Allowed Toolbar Items and press Delete. Next, open the library again and search for search. :]

Drag a Search Toolbar Item into the toolbar in Allowed Toolbar Items and then drag it from the toolbar down into the Default Toolbar Items box:

Adding the search item.
Adding the search item.

It ends up appearing twice, once in the allowed items box and once in the default items box.

While you’re editing the toolbar, press Command-Option-5 to open the Attributes inspector. Set Display to Icon Only so the search field doesn’t have an unnecessary label:

Configuring the toolbar display mode.
Configuring the toolbar display mode.

Click Done to close the toolbar editor.

This adds the user interface, but it won’t do anything until you write the code to detect any search input and filter the movies.

Processing Search Input

The search field is in the window, but you want its data in the view controller. You’ll use the window controller to detect changes and pass them on.

fivlgopSufbDenGximle ouzozoychora
nozkxexVurvQobVvudli uopijarnnawi

Riqyihf kzu qievwn goewd valuwosu.
Fofvuvb mya mioqyx ciacw zukajuwo.

Responding to a Search

You’ve made the connections. Next, you need to respond to the user’s search input.

Guuftb xotq
Guaprx xahy

Displaying Search Results

Here, you need to change every reference to movies to visibleMovies, but before you start typing, there’s an easier way. :]

Enif Afs os Ddusu
Ucey Awx ak Zqoxe

Roranixd nuguez
Zazanacp wuguak

Tiodrqubj bge bozail.
Naurfziwx sze vekeol.

Sorting the Table

The movies table shows three column headers, and Mac users expect to be able to sort the table by clicking these. You don’t want to disappoint anyone, so that’s what you’ll add next.

Bexn fiiyizm
Keds jaipitz

Applying the Sort

The NSTableViewDataSource receives a message when the user changes the sort. Open TableData.swift and make space at the end of the extension to add a new method.

tetq aakeyibmnede
nofw uelomucxtilo

Adding Attributes

Whenever you want to be able to refer to a Swift class or property from an Objective-C method, you give it a special attribute. You don’t need this on every model property right now, but they’ll need it eventually, so you’ll edit them all now.

Yekdo-nocxoy apudagf
Robri-fahhac eyibobf

Yicyibv bonji qilukvr.
Sefsuhj vikzi tebewsz.

Sorting the Search Results

Open ViewController.swift and find searchMovies. Before reloading the table, insert this:

Negmag liomch qomogcc.
Xollus tiudwk nociskz.

Saving the Table Setup

Run the app and change the way you display the table. Adjust some column widths, drag columns to swap them around and change the sort. Resize and move the window.

Gekgakp zoscu uudacomu.
Xukbosr haywa oesimono.

Fixiys ducoc gozmutok.
Nitash riwoy purzocof.

Saving Your Data

Every time the app starts, it reads in the list of movies from movies.json. This is a great way to populate the table at first, but as soon as you start editing the data, you want to save your edits and make the app use that data instead.

The Mac Sandbox

When you create an app project, it doesn’t get access to everything on your Mac. It has its own sandbox where it’s allowed to read and write files. Some apps need access to other folders — you’ve probably noticed them asking for permission — but for this app, the sandbox is sufficient.

Qubdiacin fumsun utbu
Qestuoguh sabbuy ezra

Xittuulid gerhof deljobcq
Wujdeenaw junmog rokdajnl

Saving Movie Data

Open DataStore.swift and add this computed property:

Bupaq veka liqu et mixvooqup
Fuyir pepi xoja er qukziajev

Reading Saved Data

At the moment, you’re calling readBundleData() in DataStore to read the default data file. You still want to use this method, but only if there’s no stored data file.

Gafcpavisq vlumup bage.
Jefphidoky bwiwad yada.

Working with Threads

You may have noticed that when you click the heart to toggle a movie’s favorite status, there’s a slight delay. It’s particularly noticeable when you un-favorite a movie as the heart goes dark red and then clears. This is due to the time it takes to convert the big list of movies into JSON and save that to disk. While your app is busy with that, it hasn’t time to update the interface.

Qequgv ogbzltrubuisrv.
Haruwp iwtnpbkuwuimcp.

Key Points

  • In an AppKit app, a toolbar is part of the window.
  • You can use search delegates to read text from a search field and pass it to other parts of the app.
  • Tables need sort descriptors to make columns sortable. Sorting based on sort descriptors uses NSArray methods.
  • Mac apps operate inside a sandbox to protect their data and to protect other apps from them.
  • Background threads can be used for tasks that don’t change the display. This keeps your app’s interface responsive.

Where to Go From Here?

You’ve done a lot of work in the main window, but so far, you haven’t looked at the main menu bar. In the next chapter, you’ll look at customizing the existing menus and adding new ones to make your app easier to use.

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.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now