iOS ๐ŸŽ/SwiftUI

ViewBuilder

yongmin.Lee 2022. 11. 10. 11:59

ViewBuilder

- ์ •์˜ : ํด๋กœ์ €๋กœ ๋‹ค์ˆ˜์˜ ์ž์‹ ๋ทฐ๋“ค์„ ๋งŒ๋“ค์ˆ˜ ์žˆ๊ฒŒํ•ด์ฃผ๋Š” parameter attribute

- buildBlock ์ด๋ผ๋Š” ํƒ€์ž… ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ž์‹ ๋ทฐ๋กœ ์ž‘์„ฑ๋œ ๋‹จ์ผ ๋ทฐ๋ฅผ ์ „๋‹ฌ

- 2๊ฐœ ์ด์ƒ์˜ ๋ทฐ์ผ๋•Œ๋Š” TupleView ๋ผ๋Š” ํƒ€์ž…์„ ๋ฆฌํ„ด

- body ํ”„๋กœํผํ‹ฐ๋Š” ์•”์‹œ์ ์œผ๋กœ @ViewBuilder๋กœ ์„ ์–ธ๋˜์–ด์žˆ๋‹ค

 

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— 

func contextMenu<MenuItems: View>(
    menuItems: () -> MenuItems
) -> some View

ViewBuilder๋ฅผ ๋ถ™์—ฌ์คŒ์œผ๋กœ์จ

func contextMenu<MenuItems: View>(
    @ViewBuilder menuItems: () -> MenuItems
) -> some View

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ์ž์‹๋ทฐ๋“ค์„ ํด๋กœ์ € ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑํ• ์ˆ˜ ์žˆ๋‹ค

myView.contextMenu {
    Text("Cut")
    Text("Copy")
    Text("Paste")
    if isSymbol {
        Text("Jump to Definition")
    }
}

 

 

 

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ

https://developer.apple.com/documentation/swiftui/viewbuilder