Swift Playground无法识别其他文件夹中的结构 [英] Swift Playground didn't recognize structs in other folders
问题描述
我在Swift Playground中使用带有SwiftUI的AVFoundation的AVAudioEngine,并使用MVVM作为体系结构.但是,如果我将结构分离到文件中,则会收到两个错误:执行被中断,原因:信号SIGABRT.和在范围内找不到文件.所有工作的唯一方法是在同一个文件中使用它们.
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.
这是操场上的基本代码,我称之为MusicCreatorView.
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可以通过StartView找到(如果在StartView处调用,只需将SIGABRT错误从PlaygroundPage.current.setLineView()移动到调用它的位置),但MusicCreatorView找不到.
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()
}
}
尝试在MusicCreatorView上调用AudioEngine
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>>
}
}
}
这是我的文件的组织方式 https://i.stack.imgur.com/losWf.png
Here is how my files are organized https://i.stack.imgur.com/losWf.png
推荐答案
源"中的多个文件看不到彼此.它们只是实际场所可用的独立库.您应该在一个真实的iOS项目中而不是在操场上进行开发.
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.
这篇关于Swift Playground无法识别其他文件夹中的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!