UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Cannot convert value of type 'RemoveFunctionIntent.Type' to expected argument type '() -> INIntent'

Forums > Swift

I have a series of functions in my class that I would like to register their Intents. Right now the code is very repetitive where the functions are setup like this:

    func removeFunction(numberToRemove: Int) -> Int {
        self.number += numberToRemove
        #if !os(tvOS) && !os(watchOS)
        self.logger.log("Remove Multiple")
        let intent = RemoveFunctionIntent()
        intent.number = NSNumber(value: numberToRemove)
        let interaction = INInteraction(intent: intent, response: nil)
        interaction.donate { (error) in
            if error != nil {
                if let error = error as NSError? {
                    self.logger.log("error occurred: \(error, privacy: .public)")
                }
            } else {
                self.logger.log("Remove  Intent success")
            }
        }
        self.logger.log("Remove , total = \(self.number, privacy: .public)")
        #endif
        return (self.number)
    }

I have very similar functions for Add singular, add multiple, remove singular, etc. So I tried to turn this into a simple Intent Handler that I could call. It is pretty simple

 func intentHandler(_ named: String, valueInt: Int, intentFunction: @escaping(() -> INIntent)){
        #if !os(tvOS)
        logger.log("\(named)")
        let intent = intentFunction()
        intent.suggestedInvocationPhrase = named
        intent.people = NSNumber(value: valueInt)

        let interaction = INInteraction(intent: intent, response: nil)

        interaction.donate { (error) in
            if error != nil {
                if let error = error as NSError? {
                    self.logger.log("error occurred: \(error, privacy: .public)")
                }
            } else {
                self.logger.log("\(named) Intent success")
            }
        }

        self.logger.log("\(named), value = \(valueInt, privacy: .public)")
        #endif
    }

However now I get the following error at the call site -

 func removeNumber(numberToRemove: Int) -> Int {
        self.number += numberToRemove
        intentHandler("Remove Multiple", valueInt: numberToAdd, intentFunction: RemoveFunctionIntent)
        return (self.number)
    }

Cannot find RemoveFunctionIntent in scope. Why can I find it in the first version of the function removeNumber but not in the refactored version?

** added @escaping which was also in the errors, but didn't clear the in scope error.

2      

Ok, first part solved, if you notice the #if !os(tvOS) directive, that was causing the not in scope, so moved that to the call site, but now have new error....

Cannot convert value of type 'RemoveFunctionIntent.Type' to expected argument type '() -> INIntent'

Any suggestions?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.