简体   繁体   English

Swift:声明(和初始化)类型为 function 的变量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM