简体   繁体   English

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

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

I'm using AVAudioEngine of AVFoundation with SwiftUI in Swift Playground and using MVVM as Architecture.我在 Swift Playground 中使用 AVFoundation 的 AVAudioEngine 和 SwiftUI 并使用 MVVM 作为架构。 But if I separate my structs in files, I receive two errors: Execution was interrupted, reason: signal SIGABRT.但是如果我在文件中分离我的结构,我会收到两个错误:执行被中断,原因:信号 SIGABRT。 and Cannot find file in scope.在 scope 中找不到文件。 The only way that all works is using them in the same file.唯一可行的方法是在同一个文件中使用它们。

Here is the playground base code, where I call MusicCreatorView.这是 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 can be found by StartView (if called at StartView, just move SIGABRT error from PlaygroundPage.current.setLineView() to where it is called) but can't be found by MusicCreatorView. 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()
    }

}

Trying to call AudioEngine at MusicCreatorView尝试在 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>>
        }
    }
}

Here is how my files are organized https://i.stack.imgur.com/losWf.png这是我的文件的组织方式 https://i.stack.imgur.com/losWf.png

Multiple files in Sources cannot see each other . Sources 中的多个文件不能互相看到。 They are just independent libraries available to the actual playground .它们只是可供实际操场使用的独立库。 You should be developing this in a real iOS project, rather than a playground.您应该在真正的 iOS 项目中开发它,而不是在操场上。

You just need to explicitly declare stuff as public.您只需要明确声明内容为公开。 Its annoying, and sometimes I need to close and reopen the playground for the scoping to take effect after marking them public.这很烦人,有时我需要关闭并重新打开操场,以便在将它们标记为公开后生效。

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

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