我的录音机 SwiftUI/Storyboard 应用程序在加载时不断崩溃

Posted

技术标签:

【中文标题】我的录音机 SwiftUI/Storyboard 应用程序在加载时不断崩溃【英文标题】:My Voice Recorder SwiftUI/Storyboard app keeps crashing when it loads 【发布时间】:2019-11-01 15:46:58 【问题描述】:

我正在用 SwiftUI 制作一个录音应用程序,我也在将它与一个故事板应用程序混合使用。这是我的第一个 SwiftUI 应用程序,到目前为止,我真的很喜欢使用 SwiftUI 进行编码。

但是,我遇到了一个小问题。当我启动 ContentView 时,我的代码不断崩溃。

我正在从这里做教程:https://www.blckbirds.com/post/voice-recorder-app-in-swiftui-1

我已经构建了一个 Swift Storyboard 应用程序,我想将 SwiftUI Voice Recorder 与之前构建的应用程序集成。我遵循了本教程:Is there any way to use storyboard and SwiftUI in same ios Xcode project?。

这是我的代码:

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()
    

这行得通,但是当我开始学习录音机教程并且您必须输入“@ObservedObject var audioRecorder: AudioRecorder”时,“class ViewHostingController: UIHostingController”中会出现错误,特别是在“super. init(coder: coder,rootView: ContentView());”,其中 Xcode 说“在调用中缺少参数 'audioRecorder' 的参数”。当我关注 Xcode 的 AutoFix 时,它会将该行代码更改为“super.init(coder: coder,rootView: ContentView(audioRecoder: AudioRecorder()));”。不会出现错误,但运行此程序会导致应用崩溃,并显示以下代码:“Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeec48ec8)”。

任何帮助解决这个问题真的很棒!十分感谢! :)

【问题讨论】:

【参考方案1】:

它应该是.navigationBarTitle("Voice Recorder"),(注意句号!),它应该在VStack 结束之后(下一行)。

【讨论】:

以上是关于我的录音机 SwiftUI/Storyboard 应用程序在加载时不断崩溃的主要内容,如果未能解决你的问题,请参考以下文章

如何用我的录音完成闹钟或通知

如何使用我的 apache/php 网络托管来设置收音机? [关闭]

录音机中的暂停功能

带有 React Native 的录音机

iOS:调用原生录音机

在安卓模拟器上使用录音机