IT

Swift에서 type def를 선언하려면 어떻게 해야 합니까?

itgroup 2023. 10. 25. 23:15
반응형

Swift에서 type def를 선언하려면 어떻게 해야 합니까?

스위프트에서 커스텀 타입이 필요하면,typedef, 어떻게 하죠? (닫힘 구문 유형의 def와 같은 것)

키워드를typealias대신 사용됩니다.typedef:

typealias CustomType = String
var customString: CustomType = "Test String"

위의 답변에 추가됨:

"typealias"는 typedef와 유사한 기능을 수행하는 swift로 사용되는 키워드입니다.

    /*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 */

언급URL : https://stackoverflow.com/questions/24077428/how-do-i-declare-typedef-in-swift

반응형