温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How do you create an optional variable that is assigned a function in Swift
function swift swift-optionals

其他 - 如何创建在Swift中分配了功能的可选变量

发布于 2020-06-06 15:48:30

我想创建一个可以分配一个函数的可选变量(或者可以为nil)。我猜它看起来像这样,但是以下代码无法编译。

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

谢谢你的帮助

查看更多

提问者
code kid
被浏览
8
Greg de J 2020-03-24 14:26
var variableThatWillBeAssignedAFunction: ((Int) -> Int)?

为了使其更具可读性,您还可以使用类型别名:

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