0

I'm making a voice recording app in SwiftUI, and I'm mixing it with a storyboard app too. This is my first SwiftUI app and I'm really enjoying coding with SwiftUI so far.

However, I am coming across a slight problem. My code keeps crashing when I launch the ContentView.

I am doing the tutorial from here: https://www.blckbirds.com/post/voice-recorder-app-in-swiftui-1

I have already built a Swift Storyboard app and I want to integrate the SwiftUI Voice Recorder with the previously built app. I followed this tutorial: Is there any way to use storyboard and SwiftUI in same iOS Xcode project?.

This is my code:

import UIKit
import SwiftUI

struct ContentView: View {

    @ObservedObject var audioRecorder: AudioRecorder

  var body: some View {
      VStack {
        RecordingsList(audioRecorder: audioRecorder)

        if audioRecorder.recording == false {
            Button(action: {print("Start Recording")}) {
                Image(systemName: "circle.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 100, height: 100)
                .clipped()
                .foregroundColor(.red)
                .padding(.bottom, 40)
            } //button action ends here
        } else {
            Button(action: {print("Stop Recording")}) {
                Image(systemName: "stop.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 100, height: 100)
                .clipped()
                .foregroundColor(.red)
                .padding(.bottom, 40)
            } //button stop ends here
        } //if and else ends here
        navigationBarTitle("Voice Recorder")
      } //VStack Ends here
    } //body view ends here
} //Struct ContentView Ends here

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(audioRecorder: AudioRecorder())
    }
}

class ChildHostingController: UIHostingController<ContentView> {

    required init?(coder: NSCoder) {
        super.init(coder: coder,rootView: ContentView());
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

This worked, but when I began following the tutorial for the Voice Recorder and you have to type "@ObservedObject var audioRecorder: AudioRecorder", an error would occur in "class ViewHostingController: UIHostingController", specifically at the line "super.init(coder: coder,rootView: ContentView());", where Xcode says "Missing argument for parameter 'audioRecorder' in call". When I following Xcode's AutoFix, it changes that line of code to "super.init(coder: coder,rootView: ContentView(audioRecoder: AudioRecorder()));". No error appear, but running this will cause the app to crash with the code: "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeec48ec8)".

Any help to ry and fix this issue is really great! Thank you a lot! :)

1 Answers1

0

It should be .navigationBarTitle("Voice Recorder"), (note the period!), and it should be after the closing of the VStack (one line lower).

John M.
  • 8,892
  • 4
  • 31
  • 42