If you want to replicate your Javascript code in Swift, it will be sort of like the following:
struct Class1 {
let val1: String
init(_ val1: String) {
self.val1 = val1
}
func say(val: String) {
print(val1 + " " + val)
}
}
let class1: (String) -> Class1 = { val1 in
let obj = Class1(val1)
return obj
}
let obj1 = class1("hello")
obj1.say("ken")
obj1.say("tom")
Where class1 variable holds a function to be invoked which accepts a String as parameter and returns an object of Class1 type.
Tweaking a litle bit the solution of Jean-Philippe Pellet, in order to make it more clear hopefully:
typealias ReturnType = (String) -> ()
func class1(val: String) -> ReturnType {
let returnFunction: ReturnType = { (val1: String) in
print(val + " " + val1)
}
return returnFunction
}
let obj1 = class1("hello")
obj1("ken")
obj1("tom")
where the typealias ReturnType represents the signature of a function which accepts a String and returns void.