|
Hi Developers. I want to return multiple Views using return statement. e.g
I want to have some condition based on state. before rendering. I get warning saying color.green will not be reached. which is understandable, but then how is it accepting it in normal closure? |
|
Ternary operators to the rescue
or move Color.green inside the View like so:
|
|
@ygeras adding color to VStack would break the strucutre I am looking for. Color.green and VStack content should be 50-50. also about soln #1, terneray operator is good approach if conditions are simple, but in case of complex conditions/calculations, it might not be feasible. I am trying to simulate a way where in i can have some calculations based on current views State. I come from React so in react you have certain calculations you can do based on states before return statement. I am trying to achive something similar. I am able to do that with just a single view, but it doesn't work with returning mulitple views |
|
I am not familiar with React, but most programmig languages stop/end executing code after "return" keyword. In your code you return a view and then declare another view... so XCode will warn you, that it will never execute that line. As for the view to make it take proportional screen, there are many ways to do that, as well as make necessary calculations. Narrow down your question to the parts you would like to achieve so community will be able to understand more clearly your task at hand and help :) as another approach try to make custom view that can handle more views. Break down view to pass to another view, make calculations etc... there many ways to handle the same...
Even simple removing "return" keyword makes your code work. |
|
@ygeras This is something I was looking for! I can have complex conditions on top and return multiple views. can you help me understand how is it returning views without return statement and with logic above it? and Thanks for the solution! |
|
In simple words SwiftUI revolves around structs that conform to View protocol. The protocol requires you to have property Like you can see if you run this code:
you will notice that the text structs have the same warnings that you have. This is very simplified example and explanation. There are much more happening behind the scenes, that is hidden from us :) |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.