How do view types like Text or Image conform to the View protocol in SwiftUI?

前端 未结 4 1174
后悔当初
后悔当初 2021-02-20 08:39

There is one big thing that confuses me about view types in SwiftUI:

They don\'t seem to conform to the View protocol, but somehow, they mysterious

4条回答
  •  -上瘾入骨i
    2021-02-20 08:43

    SwiftUI view types ARE conforming to the View protocol, otherwise as you mentioned the code wouldn't compile. I found some of these which were available in SwiftUI public extensions:

    Image type has an extension which directly conforms to View:

    extension Image : View {
    }
    

    Circle conforms to Shape, which itself conforms to View:

    public struct Circle : Shape {
      ...
    }
    
    public protocol Shape : Equatable, Animatable, View {
      ...
    }
    

提交回复
热议问题