[英]How do I declare typedef in Swift
如果我需要在 Swift 中使用自定义类型,我可以typedef
,我该怎么做? (类似于闭包语法 typedef)
关键字typealias
用于代替typedef
:
typealias CustomType = String
var customString: CustomType = "Test String"
添加到上面的答案中:
"typealias" 是使用的关键字 is swift,它的功能与 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")
}
要使用输入参数创建 typedef,语法如下所示:
/*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 */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.