Is it acceptable to create multiple classes in one

2020-04-01 08:29发布

问题:

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 { ... }

回答1:

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.



回答2:

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.



回答3:

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.



标签: class swift