【发布时间】:2020-09-10 12:19:32
【问题描述】:
据我所知,ViewModifier 协议的定义是这样的:
protocol ViewModifier {
// content view type passed to body()
typealias Content
// type of view returned by body()
associatedtype Body : View
// only requirement
func body(content: Self.Content) -> Self.Body
}
我的问题是:
为什么 Self.Content 是 typealias 而 Self.Body 是 associatedtype ?
有什么区别?
【问题讨论】:
-
Body将被推断 从您的返回,因此它是关联类型,Content将提供 给您,ViewModifer 知道(因为由它生成),但为你隐藏,typealias 也是如此。
标签: swiftui swift-protocols type-alias associated-types