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

SOLVED: Question: When to create Swift file and when to create SwiftUI file

Forums > 100 Days of SwiftUI

@boat  

Hello Good morning,

I'm now in days between 30-50, so I am learning on how to make more complicated apps.

I have two questions regarding apps which have more than one views (pages)

In one project:

1, Is it true that theoritically we can just write everything (all of codes for one project) in the one/main Content.View file ?

2, Is it right to say that , though point 1 is correct, for the purpose of being organized, we'd better break one big code into small chunks (separete files) , easier for us to look for and work on.

3, when do I create Swift File and when do I create SwiftUI file ? How to differentiate these two types of files , as in what they do and how they are put together with other codes?

Thank you in advance,

Boat

2      

1, Is it true that theoritically we can just write everything (all of codes for one project) in the one/main Content.View file ?

Yes

2, Is it right to say that , though point 1 is correct, but for the purpose of being organized, we'd better break one big code into small chunks (separete files) , easier for us to look for and work on.

Absolutely, yes

3, when do I create Swift File and when do I create SwiftUI file ?

Create a SwiftUI file when you are writing a View.

Create a Swift file when you are creating other kinds of code.

Although, really, it doesn't much matter. A "SwiftUI file" isn't a special kind of file different from a "Swift" file. They are both .swift files. It's just that Xcode has a template for SwiftUI that includes a skeletal View struct for you to start with. You can create a new file using either the SwiftUI template or the Swift file template, you just might need to type more stuff on your own.

For instance, I sometimes start a SwiftUI View from a regular Swift file template and manually type in the stuff that Xcode gives you for free in the SwiftUI template. Conversely, I sometimes create a new file from the SwiftUI template even though I'm not writing a SwiftUI View, and I just delete the stuff I don't need.

3      

@boat  

Thank you @roosterboy

One step further :

If everything is in one big file, then we don't need to use @published or that sort of wrappers, which is meant to help carry values across different files.

Have a nice day/evening :)

Boat

2      

If everything is in one big file, then we don't need to use @published or that sort of wrappers, which is meant to help carry values across different files.

@Published has nothing to do with files. It's to expose variables to another object in a way that changes to that variable can be tracked and used by another object (usually a SwiftUI View). Those other objects can be the same file or another file, doesn't matter. What matters is how you hook them up in your code.

3      

@boat  

got it. Thank you @roosterboy.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.