Search code examples
iosswiftswiftuiswift-protocols

Why can't you rename the body property?


In swiftUI, in order to conform to the View protocol, the conforming struct has to implement the body property (specifically, its getter, since body property is read-only).

Can I change the name of body property to something else, say presentation?

struct ContentView: View {
    var presentation: some View {
        Button("Hello SwiftUI!") {
            
        }
    }
}

This doesn't work. I get 'Type 'ContentView' does not conform to protocol 'View'.

In UIKit, when conforming to the UIApplicationDelegate protocol, I was able to change the name of the UIWindow variable. The documentation has the name 'window', but when conforming, I changed it to mainWindow and things worked.

Why can't I change the name of body property of a swiftUI view? Am I missing something?


Solution

  • Can I change the name of body property to something else

    No. A protocol's requirements are required. To conform, you must obey them to the letter.

    when conforming to the UIApplicationDelegate protocol, I was able to change the name of the UIWindow variable

    That variable isn't a requirement. Note the marking optional on the page you yourself linked to.