Home > Software design >  How to create separate data mapping file with function names in Swift
How to create separate data mapping file with function names in Swift

Time:10-21

I have a macOS app that I'm creating in Swift and I have integrated an external HID device that has a number of controls on it. The HID part is done where I am receiving all of the hid commands from the device and I am trying to create a mapping file where I can maintain the HID key mappings in a separate swift file. All I want in that file is the data and what I want to do is this;

  • raw hid data is received from HID device (In ViewController)
  • Lookup the function name assigned to this hid data (In separate file)
  • Run the function that is mapped to that key. (Function located in the main ViewController)

So far I have the external swift file setup with all of the mapping and that all works fine but my issue is when I try to call the looked up function in the ViewController, it says the function can't be found in the scope.

Initially I thought I would use a delegate but the external file isn't a viewcontroller, just a separate swift file so I don't know if I can do that?.

I've tried searching but everything I've found is calling a function from another ViewController which I'm not. It's very possible I'm not using the best approach and my goal is to just keep all of the mapping in a separate file as there is a lot and it woudl be easier to maintain.

Any suggestions are appreciated.

CodePudding user response:

You can simply create a UIViewController as the external file and add it as a property to the main ViewController.

In the external file add this.

@IBOutlet var uiViewController: UIViewController!

In the ViewController add this.

var externalFileViewController: UIViewController!

override func viewDidLoad() {
    super.viewDidLoad()

    externalFileViewController = externalFileViewController?.loadView()

    // If we have an object then load it
    if let viewController = externalFileViewController {
        viewController.view.frame = view.frame
        viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        view.addSubview(viewController.view)
        uiViewController = viewController

    }
}

Now in the viewController look up the functions to be called from the external file and call them using the function name.

The functions are defined in the external file using @IBAction.

Let me know if you have any questions.

CodePudding user response:

This is one way to achieve this. It can get tedious. You can totally skip writing out a separate protocol for the delegate, but this is cleaner design.

protocol HIDMessageDelegate: AnyObject {
  // example messages
  func message1()
  func message2()
  func message3()
}

class HIDMessageParser {
  static weak var delegate: HIDMessageDelegate?

  static func parseHIDMessage() {
    var condition = 0
    // this is where your switch statement will go and you'll parse things and call the relevant delegate method
    switch (condition) {
    default:
      delegate?.message1()
    }
  }
}

class MyViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    HIDMessageParser.delegate = self
  }
}

extension MyViewController: HIDMessageDelegate {
  func message1() {

  }

  func message2() {

  }

  func message3() {

  }
}
  • Related