我发现SwiftUI框架有些奇怪。 让我们关注这个例子:
let view = VStack {
Text("Hello")
}
view.body // Compile Error: Value of type 'VStack<Text>' has no member 'body'
但是当我查看VStack
的声明时:
@frozen public struct VStack<Content> : View where Content : View {
@inlinable public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content)
public typealias Body = Never
}
还有View
的声明:
public protocol View {
associatedtype Body : View
var body: Self.Body { get }
}
我希望view
符合协议View
,并应采用方法body
,但不能!
我想错了吗?