zack2012
(Zack2012)
1
I found there are many public struct in SwiftUI conforming to View protocol, but I can't call their body property, e.g:
let image = Image("")
print(image.body) // Error: Value of type 'Image' has no member 'body'
but View is public protocol, Image is public struct, the body property must be public, why can't I call it?
1 Like
svanimpe
(Steven Van Impe)
2
I'm not entirely sure myself, but I assume some SwiftUI views are "leaf" views that either don't conform to View at all, or have a body of type Never.
If all views were required to have a body that is also a view, that would cause an infinite recursion.
1 Like
aasimk
3
There are primitive views Text, Color, Image, Shape, Divider and Spacer from which other views are derived from. This was briefly covered in the SwiftUI Essentials session at WWDC.
1 Like
zack2012
(Zack2012)
4
you can find Image definition:
public struct Image : Equatable {
//...
}
extension Image {
public typealias Body = Never
}
extension Image : View {
}
If all views were required to have a body that is also a view, that would cause an infinite recursion.
This is not correct. you can use other existed type to conform to protocol. In SwiftUI Never is that type. in the swiftUI.swiftinterface file, there is something below:
extension Never : View, _UnaryView {}
extension Never {
public typealias Body = Never
public var body: Never {
get
}
}
View is protocol require body property, Image conforms to View, they are all public, but Image doesn't have body property. Is there something new complier feature for SwiftUI?
1 Like
jonprescott
(Jonathan Prescott)
5
In the extension for Never, a computed property Body is defined. That satisfies the View protocol requirements.
1 Like