繁体   English   中英

Swift Playground 无法识别其他文件夹中的结构

[英]Swift Playground didn't recognize structs in other folders

我在 Swift Playground 中使用 AVFoundation 的 AVAudioEngine 和 SwiftUI 并使用 MVVM 作为架构。 但是如果我在文件中分离我的结构,我会收到两个错误:执行被中断,原因:信号 SIGABRT。 在 scope 中找不到文件。 唯一可行的方法是在同一个文件中使用它们。

这是 Playground 的基本代码,我称之为 MusicCreatorView。

import SwiftUI
import PlaygroundSupport

public struct StartView: View {

    public var body: some View {
        ZStack {
            Rectangle()
                .fill(Color.white)
                .frame(width: 400, height: 400, alignment: .center)
            NavigationView {
                VStack {
                    NavigationLink("Start", destination: MusicCreatorView())
                }
            }
        }
    }
}

PlaygroundPage.current.setLiveView(StartView()) // error: Execution was interrupted, reason: signal SIGABRT.

AudioEngine 可以通过 StartView 找到(如果在 StartView 调用,只需将 SIGABRT 错误从 PlaygroundPage.current.setLineView() 移动到调用它的位置)但 MusicCreatorView 找不到。

import Foundation
import AVFoundation

public struct AudioEngine {
    public var engine = AVAudioEngine()
    public var player = AVAudioPlayerNode()
    public var audioBuffer: AVAudioPCMBuffer?
    public var audioFormat: AVAudioFormat?
    public var audioFile: AVAudioFile? {
        didSet {
            if let audioFile = audioFile {
                audioFormat = audioFile.fileFormat
            }
        }
    }
    public var audioFileURL: URL? {
        didSet {
            if let audioFileURL = audioFileURL {
                audioFile = try? AVAudioFile(forReading: audioFileURL)
            }
        }
    }

    public init() {
        setupAudio()
    }

    public mutating func setupAudio() {
        audioFileURL = Bundle.main.url(forResource: "EverybodysCirculation", withExtension: "mp3")
        guard let format = audioFormat else { return }
    
        engine.attach(player)
        engine.connect(player, to: engine.mainMixerNode, format: format)
        engine.prepare()
    
        do {
            try engine.start()
        } catch {
            print(error.localizedDescription)
        }
    }

    public func scheduleAudioFile() {
        guard let audioFile = audioFile else { return }
    
        player.scheduleFile(audioFile, at: nil, completionHandler: nil)
    }

    public func playSound() {
        player.isPlaying ? player.pause() : player.play()
    }

}

尝试在 MusicCreatorView 调用 AudioEngine

import Foundation
import SwiftUI
import AVFoundation

public struct MusicCreatorView: View {
    var audioEngine = AudioEngine() // Cannot find 'AudioEngine' in scope

    public init() {}

    public var body: some View {
        Text("Try to Create your own music")
        Button("play") {
            print("apertou")
            audioEngine.playSound() // audioEngine <<error type>>
        }
    }
}

这是我的文件的组织方式 https://i.stack.imgur.com/losWf.png

Sources 中的多个文件不能互相看到。 它们只是可供实际操场使用的独立库。 您应该在真正的 iOS 项目中开发它,而不是在操场上。

您只需要明确声明内容为公开。 这很烦人,有时我需要关闭并重新打开操场,以便在将它们标记为公开后生效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM