Home > database >  swift objective-c error Cannot convert value of type () -> () to specified type MIDI.IO.Manager
swift objective-c error Cannot convert value of type () -> () to specified type MIDI.IO.Manager

Time:05-29

I am getting error: Cannot convert value of type '() -> ()' to specified type 'MIDI.IO.Manager'

I am going to be exposing the value create by the midiManager to React Native. Just don't understand Why I get the error with this code block

I am not sure how to fix this as I am new to swift and objective-c. Any help would be greatly appreciated

import Foundation
import MIDIKit

@objc(MidiManager)
class MidiManager: NSObject{
  
  var midiManager: MIDI.IO.Manager = {
          let newManager = MIDI.IO.Manager(
              clientName: "MIDIEventLogger",
              model: "LoggerApp",
              manufacturer: "Orchetect") { notification, manager in
                  print("Core MIDI notification:", notification)
              }
          do {
              logger.debug("Starting MIDI manager")
              try newManager.start()
          } catch {
              logger.default(error)
          }
    }
  

}

enter image description here

CodePudding user response:

If you wanted computable property, then

var midiManager: MIDI.IO.Manager {   // 1) remove =
      let newManager = MIDI.IO.Manager(
          clientName: "MIDIEventLogger",
          model: "LoggerApp",
          manufacturer: "Orchetect") { notification, manager in
              print("Core MIDI notification:", notification)
          }
      do {
          logger.debug("Starting MIDI manager")
          try newManager.start()
      } catch {
          logger.default(error)
      }
      return newManager   // 2) return object
 }

if you wanted initialised with default value then

var midiManager: MIDI.IO.Manager = {
      let newManager = MIDI.IO.Manager(
          clientName: "MIDIEventLogger",
          model: "LoggerApp",
          manufacturer: "Orchetect") { notification, manager in
              print("Core MIDI notification:", notification)
          }
      do {
          logger.debug("Starting MIDI manager")
          try newManager.start()
      } catch {
          logger.default(error)
      }
      return newManager   // 1) return object
 }()    // 2) make call

CodePudding user response:

You're assigning a closure of type () -> Void to a variable of a type MIDI.IO.Manager. You need to change a closure a bit and then actually call it:

var midiManager: MIDI.IO.Manager = {
      let newManager = MIDI.IO.Manager(
          clientName: "MIDIEventLogger",
          model: "LoggerApp",
          manufacturer: "Orchetect") { notification, manager in
              print("Core MIDI notification:", notification)
          }
      do {
          logger.debug("Starting MIDI manager")
          try newManager.start()
      } catch {
          logger.default(error)
      }
      return newManager
}()

But personally I don't think that starting a manager already after an initializing is a good idea. If it fails with an error and you return it, application logic may break in some place.

Also you may want to make MIDI.IO.Manager let variable, i.e. immutable one.

  • Related