简体   繁体   English

Swift 在范围内找不到变量

[英]Swift Cannot find variable in scope

Swift giving error that var is not in scope. Swift 给出 var 不在范围内的错误。 "Cannot find 'midiManager' in scope" I am not sure how to fix issue “在范围内找不到 'midiManager'”我不确定如何解决问题

menu.swift菜单.swift

struct MainMenuBar: MenuBar {
    public var body: StandardMenuBar {
        StandardMenu(title: "Midi Devices") {
            midiManager.endpoints.outputs.forEach { device in
                TextMenuItem(title: device.name) { _ in
                    print(device.uniqueID)
                }
            }
        }
    }
}

midi.swift midi.swift

@objc(ReactNativeMidiModule)
class ReactNativeMidiModule: RCTEventEmitter, NSApplicationDelegate {
  // Create midi manager
  public let midiManager = MIDI.IO.Manager(
    clientName: "MIDIEventLogger",
    model: "ELMC Midi Monitor",
    manufacturer: "ELMC")

  // other code
}

在此处输入图像描述

midiManager is not present inside your MainMenueBar . midiManager不存在于MainMenueBar中。 So midiManager can not be found.所以midiManager You must need to add midiManager inside your MainMenueBar structure.您必须需要在MainMenueBar结构中添加midiManager Like this -像这样 -

struct MainMenuBar: MenuBar
{
    public let midiManager = MIDI.IO.Manager(clientName: "MIDIEventLogger", model: "ELMC Midi Monitor", manufacturer: "ELMC")
    public var body: StandardMenuBar
    {
        
        StandardMenu(title: "Midi Devices")
        {
            midiManager.endpoints.outputs.forEach { device in
                TextMenuItem(title: device.name) { _ in
                    print(device.uniqueID)
                }
            }
        }
    }
}

or By creating a common class as-通过创建一个通用类作为-

class MidiManager {
    static let midiManager = MIDI.IO.Manager(clientName: "MIDIEventLogger", model: "ELMC Midi Monitor", manufacturer: "ELMC")
}

And then use it in both MainMenuBar and ReactNativeMidiModule as-然后在MainMenuBarReactNativeMidiModule中使用它作为-

struct MainMenuBar: MenuBar {
    public let midiManager = MidiManager.midiManager
    public var body: StandardMenuBar {
        StandardMenu(title: "Midi Devices") {
            midiManager.endpoints.outputs.forEach { device in
                TextMenuItem(title: device.name) { _ in
                    print(device.uniqueID)
                }
            }
        }
    }
}

@objc(ReactNativeMidiModule)
class ReactNativeMidiModule: RCTEventEmitter, NSApplicationDelegate {
  // Create midi manager
  public let midiManager = MidiManager.midiManager

  // other code
}

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

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