Home > Software engineering >  Swift files for new class or struct
Swift files for new class or struct

Time:09-24

I am new to Swift development and wanted to ask a question related to Swift files.

If I am making a project is it a good or general practice to use only one struct or class in one file? Do you need to create a different .swift file for every new struct or class ?

CodePudding user response:

It's not necessary. But create a different .swift file for every new struct or class help you more easy to maintain source code when your codebase is large

CodePudding user response:

It's common to make a new file per type (struct/class/enum) with the file named for the type, but there are exceptions to the rule. If the type definition is small, and closely related to some other type, then it can be easier to read if it's in the same file with the other type definition - that's quite common with enums. Or if the type is nested in an enclosing type, then it's often written inline. But this is a matter of personal preference.

  • Related