我如何在Swift中声明typedef
如果我需要Swift中的自定义types,那么我可以inputtypedef
,我该怎么做呢? (类似于一个闭包语法typedef)
关键字typealias
用于代替typedef
typealias CustomType = String var customString:CustomType = "Test String"
添加到上面的答案:
“typealias”是使用的是与typedef类似的函数的关键字。
/*defines a block that has no input param and with void return and the type is given the name voidInputVoidReturnBlock*/ typealias voidInputVoidReturnBlock = () -> Void var blockVariable :voidInputVoidReturnBlock = { println(" this is a block that has no input param and with void return") }
要使用input参数创buildtypedef,语法如下所示:
/*defines a block that has input params NSString, NSError! and with void return and the type is given the name completionBlockType*/ typealias completionBlockType = (NSString, NSError!) ->Void var test:completionBlockType = {(string:NSString, error:NSError!) ->Void in println("\(string)") } test("helloooooooo test",nil); /*OUTPUTS "helloooooooo test" IN CONSOLE */