简体   繁体   中英

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. But if I separate my structs in files, I receive two errors: Execution was interrupted, reason: signal SIGABRT. and Cannot find file in scope. The only way that all works is using them in the same file.

Here is the playground base code, where I call 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.

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

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

Multiple files in Sources cannot see each other . They are just independent libraries available to the actual playground . You should be developing this in a real iOS project, rather than a playground.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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