Search code examples
swiftseguemlmodel

How to access ViewController's class functions from another class?


I have this function in the ViewController class:

func detect(image: CIImage) {
    guard let model = try? VNCoreMLModel(for: Resnet50().model)
        else {
            fatalError("Loading CoreML Model Failed.")
    }

    let request = VNCoreMLRequest(model: model) {
        (request, error) in
        guard let result = request.results as? [VNClassificationObservation]
            else {
                fatalError("Model failed to process image.")
        }
        print(result.first?.identifier as Any)
        self.imageLabel.text = result.first?.identifier
    }

    let handler = VNImageRequestHandler(ciImage: image)
    do {
        try handler.perform([request])
    } catch {
        print(error)
    }
}

I'm trying to use the result.first?.identifier to do something like this in another class called SecondViewController:

if(result.first?.identifier == "flowers") {
    self.infoLabel.text = "info about flowers"
}

but I don't know how to access the function in the other class. I just started coding in Swift.


Solution

  • I found a link that helped me to solve this problem I was encountering, I'm going to share it here if anyone ever needs it in the future.