import SwiftUI /// 最小的可渲染视图:`body` 每次被求值都会给 `counter` 加一。 public struct CountedLabel: View { public let text: String public let counter: BodyCounter public init(text: String, counter: BodyCounter) { self.text = text self.counter = counter } public var body: some View { counter.mark() return Text(text) } } /// 条件分支会生成 `_ConditionalContent`,类型随分支变化。 public struct ConditionalDemo: View { public let flag: Bool public init(flag: Bool) { self.flag = flag } public var body: some View { VStack(alignment: .leading, spacing: 8) { Text("标题") if flag { Text("分支 A") } else { Text("分支 B") } } } } /// 用 `@ViewBuilder` 把多个视图组合成一个值。 @ViewBuilder public func pair(_ first: String, _ second: String) -> some View { Text(first) Text(second) } /// 擦除类型后的视图:身份与具体类型都丢失。 public enum Erasure { public static func boxed(_ text: String) -> AnyView { AnyView(Text(text)) } public static func concrete(_ text: String) -> Text { Text(text) } }