Warm tip: This article is reproduced from stackoverflow.com, please click
function swift swift-optionals

How do you create an optional variable that is assigned a function in Swift

发布于 2020-06-05 12:30:55

I would like to create an optional variable that can be assigned a function ( or can be nil). I'm guessing it would look something like this but the following code does not compile.

var variableThatWillBeAssignedAFunction: {(Int) -> Int}?

Thanks for your help

Questioner
code kid
Viewed
10
Greg de J 2020-03-24 14:26
var variableThatWillBeAssignedAFunction: ((Int) -> Int)?

To make it more readable, you can also use a typealias:

typealias IntegerTransform = (Int) -> Int
var variableThatWillBeAssignedAFunction: IntegerTransform?