Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it acceptable to create multiple classes in one .swift file, or should I create a separate .swift file for each class?

Tags:

class

swift

Is it considered poor technique to create multiple classes in one swift file, or is it best to create a separate swift file for each class?

For example, which is better:

A. Multiple classes in ViewController.swift:

1. alpha.swift:

class alpha: UIViewController { ... }
class beta: UIWebView { ... }
class gamma: UINavigationController { ... }

B. Separate .swift files for each class:


1. In alpha.swift:

class alpha: UIViewController { ... }

2. In beta.swift:

class beta: UIWebView { ... }

3. In gamma.swift:

class gamma: UINavigationController { ... }
like image 912
kmiklas Avatar asked Jun 13 '14 18:06

kmiklas


People also ask

Can you have multiple classes in a CS file?

As Paolo said,there is no benefit keeping many classes in a single file. Putting them in separate files will help you and any other programmer to find the cs file for a class quickly. By all that is right in the world, yes, it is correct!

Can we have multiple classes in same javascript file?

In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.

Can you have multiple classes in a PHP file?

PHP doesn't support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it.

When should you use classes over structs Swift?

Use Classes When You Need to Control Identity Classes in Swift come with a built-in notion of identity because they're reference types. This means that when two different class instances have the same value for each of their stored properties, they're still considered to be different by the identity operator ( === ).


3 Answers

Short answer: it depends.

Long answer:
If you have small short classes that are strongly binded together, it's correct to put the in the same file.
If you have long, unrelated classes, then you better put them in different files.

like image 150
Emilie Avatar answered Sep 20 '22 17:09

Emilie


It's not a poor technique, IF the classes are connected.

To decide if they are connected, ask: Can one class be used without the other?

If yes, then you should have two different files, since you might need to use just one of the two.


For example, in C++, collections have an inner class for their iterators. (I know it's a C++ example, but the question isn't really language related).

Though if the classes have nothing to do with each other (being on the same view doesn't count), then they should each have their separate classes.

like image 43
Lord Zsolt Avatar answered Sep 19 '22 17:09

Lord Zsolt


I have different answer for this question based on painful debugging and searching in the internet for last few days. I'm c++ dev with more than 15 years experience. Coming from this language I'm familiar with few design techniques which needs protected access. Since Swift doesn't support it and as it turns out they won't support it in near future, I've start using private access and write few classes in the same file. That way I've workaround the missing protected modifier (private functions are visible in the same file, so they will be visible for all classes in the same file, and if these classes are derived classes the private is actually working as protected). Well everything is fine and I was happy before found out that my application crashed with EXC_BAD_ACCESS code=1 ... The exceptions was not because of my code, it was because it layout of the members were somehow wrong. For example if i called one function a() from the derived class through instance variable the func b() was called. The b() was also member of the same class and was define before the a(). That is why some functions thrown bad access exception. Instance pointer was corrupt. After I moved all 3 classes to independent files everything looked fine.

Not sure that this was the actual reason or I've done something wrong, but not 100% of cases when you define multiple classes in the same file will be with defined behaviour. May be that is compiler problem, Swift is young language and even that I'm testing with Gold Master studio which is supposed to be stable, there are still a lot of bugs.

like image 23
devfreak Avatar answered Sep 18 '22 17:09

devfreak