You can't create an anonymous class in Swift.
In Kotlin, using fun interface
-s you can use a more convenient syntax to describe the creation of anonymous lambdas:
fun interface FunInterfaceExample {
fun singleFunctionInInterface(funInterfaceExample: String): String
}
interface NotFunInterface {
fun singleFunctionInInterface(funInterfaceExample: String): String
}
fun example() {
val notFun = object : NotFunInterface {
override fun singleFunctionInInterface(funInterfaceExample: String): String {
return "return"
}
}
val listener = FunInterfaceExample {
println("it: ${it}")
"some return value"
}
}
Swift does not have a similar syntax for describing anonymous classes. You can, of course, do it as in one of the answers on StackOverflow, but it seems like some kind of big overhead:
class EmptyClass {
var someFunc: () -> () = { }
init(overrides: (EmptyClass) -> EmptyClass) {
overrides(self)
}
}
// Now you initialize 'EmptyClass' with a closure that sets
// whatever variable properties you want to override:
let workingClass = EmptyClass { ec in
ec.someFunc = { print("It worked!") }
return ec
}
workingClass.someFunc() // Outputs: "It worked!"