86

Swift でカスタム タイプが必要な場合はtypedef、どうすればよいですか? (クロージャー構文の typedef のようなもの)

4

2 に答える 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 に答える