Если мне нужен пользовательский тип в Swift, я мог бы typedef
, как мне это сделать? (Что-то вроде синтаксиса закрытия typedef)
Как объявить typedef в Swift
Ответ 1
Ключевое слово typealias
используется вместо typedef
typealias CustomType = String
var customString:CustomType = "Test String"
Ответ 2
добавлен в ответ выше:
"typealias" используется для использования ключевого слова 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 */