Cracking SwiftUI: Implementing a Delegate

Tianna Lewis
4 min readOct 28, 2019

--

Background

Okay, so I finally jumped into SwiftUI. A little late to the party, because my late-2012 model MacBook couldn’t get dressed any quicker. It’s my only machine and I can’t risk anything happening to it so I tend to air on the side of caution and wait until official releases. It might be me being too cautious but it has kept me out of danger thus far.

For my first foray into SwiftUI, I decided to try to follow the Hacking With Swift Whistle Project. This project is not a SwiftUI tutorial per se but stay with me here. I also wanted to get a grasp on CloudKit and it seemed like I could accomplish both with this project. The thing is though that the article is written for UIKit. So it gives me a rough idea of what I am supposed to be accomplishing on the UI side, but I have to figure the rest out on my own. I find working through tutorials this way to be a bit more rewarding at this stage because at least I have to do at least some problem solving where the answer is handed to me.

Scenario

So I was coming along well and good for not more than a few minutes before I ran into my first big roadblock. The project called for AVAudioRecorder. Previously I would’ve counted on a ViewController to implement this delegate and conform to its protocols, but quickly realized that I could not follow the same workflow in SwiftUI. What ensued was a laborious process to try to figure out how to implement AVAudioRecorder. Now at this point in the story, you're probably asking, I am sure you could have just followed the Apple Tutorial on SwiftUI. And I would respond to this with a simple “I Did!”, or at least I tried to. I, like everyone else, stumbled back to the Landmark App Tutorial and reviewed the Interfacing with UIKit section, but honestly, for whatever reason, this just did more to confuse me. So how did I manage to get it done, you ask? Well aside from a lot of staring at my screen and thinking “Why me, why all the time me?!”, there was a lot of trial and error (like any honest developer would tell you). But after a good session of questioning my reading comprehension abilities (because come on it has to be, anyone else would have surely figured this out by now), I landed on a solution that works.

Code

First I created a new AudioRecorder class, importing SwiftUI and AVFoundation. Next step was to ensure that the AudioRecorder class was a subclass of NSObject, ObservableObject and AVAudioRecorderDelegate (the one we all come for). After that, I could implement my variables and class functions as I pleased.

AudioRecorder.swift

Next was to create an AudioRecorderView.swift file (at this point I had already created this so some changes needed to be made) to be able to observe the AudioRecorder class by making it an ObservedObject. This explains why in AudioRecorder.swift we ensured it was a subclass of ObservableObject. Doing this means I can then access the various variables and functions in the AudioRecorder class within the view.

AudioRecorderView.swift

Now if you took a close look you would have noticed that two of the variables in the AudioRecorder.swift file are marked as @Published this allows us to access these variables outside of the class so don’t forget this part if you need to be able to read and manipulate any variables in the View.

Summary

So there you go the rundown on how I tackled a problem. Now I am sure what I did here was super simple, and anyone with a week of Swift under their belt should have figured this out with ease (sarcasm alert). But I did the usual google search and read a couple of stack overflow posts, which only served to confuse me further. As someone who has had to learn most of what I know on my own, I understand that it usually is just a matter of your learning style aligning with someone else’s teaching style so hopefully, this is the answer for at least one person.

If you’re ever interested in watching me fail over and over again I stream that over on Twitch for the enjoyment of others (sometimes I succeed, which is cool).

https://www.twitch.tv/nyxdev

Disclaimer: I do not claim to be an expert , just someone who is figuring it out as I go along and want to share what I find along the way. If you know of another/better way to accomplish the problem then help us all out and share it in the comments.

--

--

Tianna Lewis

Learning and Building in the Open. Check out what I’ve been up to at tiannahenrylewis.com.