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