I work with legacy code and i find following code:
let actions = [PrinterReportType.z: {
printer.printZReport($0)
DeviceFabric.lifehubTerminal().reconciliation()
}, .x: printer.printXReport, .openSession: printer.openSession}]
"Actions" is declared like this:
let actions: [PrinterReportsModel.PrinterReportType : ((String?) -> ()) -> ()]
Key is enum value, but i can't figure out what is value here. I have new enum type of PrinterReportsModel.PrinterReportType
, and i simply want to add new value to that dictionary. I suppose this is some kind of function. So, i want to declare that function, add it here, but i can't figure out how. And i can't figure out wha type is - ((String?) -> ()) -> ()
String
is a collection of characters.
String?
means the same as Optional<String>
, so it is either Optional.none
or Optional.some(String)
.
(String?) -> ()
is a function that takes a String?
and returns nothing ( ()
also called Void
, the zero-element tuple). Let's call this as a String?
-consumer : it takes a String?
and does something with it. Maybe it just throws the String?
away. Maybe it prints it. Maybe it stores the String?
in a variable or a database or sends it over the network.
You could define a String?
-consumer closure like this:
let action: (String?) -> () = { (_ s: String?) -> () in
print(s ?? "(none)")
}
(I'm fully specifying the types above, but you could omit some of the types and let the compiler infer them.)
You could define a String?
-consumer function like this:
func test(_ s: String?) -> () { print(s ?? "(none)" }
And pass it around like this:
let action: (String?) -> () = test
You could define a String?
-consumer method in a class (or struct) like this:
class MyObject {
func test(_ s: String?) -> () { print(s ?? "(none)") }
}
And pass it around like this:
let myObject = MyObject()
let action: (String?) -> () = myObject.test
((String?) -> ()) -> ()
is a function that takes a String?
-consumer and returns nothing. You can think of this as a String?
-consumer-consumer. Maybe it throws the String?
-consumer away. Maybe it stores the String?
-consumer in a variable for later. Maybe it calls the String?
-consumer once, or ten times, or once for each element in some array of strings. Maybe it schedules a call to the String?
-consumer every second until the program exits.
You could define a String?
-consumer-consumer closure like this:
let action: ((String?) -> ()) -> () = { (_ sc: (String?) -> ()) -> () in
sc("hello")
sc(nil)
sc("world")
}
You could call the closure like this:
action({ (_ s: String?) -> () in print(s ?? "(none)")}
Or like this:
let printString: (String?) -> () = { print($0) }
action(printString)
You could define a String?
-consumer-consumer function like this:
func test(_ sc: (String?) -> ()) -> () {
sc("hello")
sc(nil)
sc("world")
}
And pass it around like this:
let action: ((String?) -> ()) -> () = test
You could define a String?
-consumer-consumer method in a class (or struct) like this:
class MyObject {
func test(_ sc: (String?) -> ()) -> () {
sc("hello")
sc(nil)
sc("world")
}
}
And pass it around like this:
let myObject = MyObject()
let action: ((String?) -> ()) -> () = myObject.test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.