UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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....

2      

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

3      

@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!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.