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.