[英]Swift: Declaring (and initialising) a variable of type function
I'd like to declare a variable of type (Int)->String
, to be used later in the code.我想声明一个
(Int)->String
类型的变量,以便稍后在代码中使用。 How to do this?这个怎么做?
This code:这段代码:
var myfunc: (Int)->String = nil
gives an error.给出一个错误。
Thank you谢谢
The variable must be optional type to accept nil
.变量必须是可选类型才能接受
nil
。 Try:尝试:
var myfunc: ((Int) -> String)?
Note: You don't need = nil
here, it's redundant.注意:这里不需要
= nil
,它是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.