NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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

Forums > 100 Days of SwiftUI

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

1      

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.

2      

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

1      

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.

2      

got it. Thank you @roosterboy.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.