如果我需要 Swift 中的自定义类型,我可以typedef
,我该怎么做?(类似于闭包语法 typedef)
问问题
39590 次
2 回答
149
关键字typealias
用于代替typedef
:
typealias CustomType = String
var customString: CustomType = "Test String"
于 2014-06-06T08:39:15.930 回答
16
添加到上面的答案:
“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")
}
要使用输入参数创建 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 */
于 2014-11-23T21:19:22.650 回答