Geon

Write Swift macros(1) 본문

SwiftUI/지식 창고

Write Swift macros(1)

jgkim1008 2024. 11. 12. 23:24

 

Swift Macros

Swift 매크로를 사용하면 컴파일 타임에 반복적인 코드를 생성하여 앱의 코드베이스를 더욱 표현적이고 읽기 쉽게 만들수 있다.

public struct StringifyMacro: ExpressionMacro {
    public static func expansion(
        of node: some FreestandingMacroExpansionSyntax,
        in context: some MacroExpansionContext
    ) -> ExprSyntax {
        guard let argument = node.argumentList.first?.expression else {
            fatalError("compiler bug: the macro does not have any arguments")
        }

        return "(\(argument), \(literal: argument.description))"
    }
}

 

 

출저: https://developer.apple.com/videos/play/wwdc2023/10166

'SwiftUI > 지식 창고' 카테고리의 다른 글

Write Swift macros(2)  (0) 2024.11.13
Discover Observation in SwiftUI  (0) 2024.11.11
Demystify SwiftUI performance  (0) 2024.11.10
Explore UI animation hitches and the render loop  (0) 2024.11.09
Image DownSampling  (1) 2024.11.08