NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Day 26: Why does DatePicker need return when var components is declared in var body?

Forums > 100 Days of SwiftUI

Hi,

I'm having difficulty understanding what's going on in Day 26 with the return keyword on DatePicker()

At the beginning of the video, when DatePicker is used on its own, it works without the return keyword.

But as soon as we start adding code such as let components = DateComponents().... or let formatter = DateFormatter() then we get the error

Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

Adding return clears the error and the code works.

Unless I've missed it, the video doesn't explain why the return is needed is this case (it's just typed in without comment in the video and it's not mentioned in the text). I wondered if it's because DatePicker isn't actually a struct/enum/class type and therefore needs different handling, but in that case, why does it work when let components.../formatter... etc are absent?

More generally, how do you know which views require return and which don't?

Thanks both for the course (which is very helpful) and for any help....

   

hi,

your question really has nothing to do with DatePicker(), per se, but it is just Swift function syntax. the documentation for functions states that

If the entire body of the function is a single expression, the function implicitly returns that expression.

in other words, Swift functions normally have a return; but return can be omitted in the case above.

when it comes to SwiftUI, you are writing Swift syntax. a simple body property for a View is a Swift function and can be written as

var body: some View {
  return Text("Hello World!")
}

but it has become customary to leverage implicit return syntax to simply write

var body: some View {
  Text("Hello World!")
}

placing an if statement in the first version of body above makes perfect sense:

var body: some View {
  let components = ......
  return Text("Hello World!")
}

but now the body consists of more than a single expression, so you cannot omit the return.

hope that helps,

DMG

1      

@delawaremathguy

Thanks very much for your detailed reply: it explained a lot.

I knew about return being required if there were multiple expressions in general, but had assumed (without thinking too deeply about it...) it didn't apply to Views, because, for example, a NavigationView / VStack / HStack etc can return several views without return.

Of course, there's a difference between the implementation of the body within ContentView, and calling a View, but I'd missed it. It makes sense now -- thanks!

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Ever ask for help and your reviewer immediately notices issues you missed? Fernando Olivares is a 10-year veteran from Big Nerd Ranch and Lambda School who can help hone your app building skills, and he's just launched a new book that does just that – use the code "hacking" to save $5!

Buy the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.