Swift Classes in One File [closed]

2019-09-04 08:31发布

问题:

I know in python it is good practice to have a models.py and put all of an application's model classes in there. Does the same apply to swift or is it better to have a separate file for each model class?

Thanks!

回答1:

Apple recommends to have separate files for each model class however it's possible to use the "Python style"



回答2:

Assuming that your project is average..I would put each of your models in a separate file. Sometimes you end up with some small files, but I would rather go through that then some massive file where I can't find anything.



回答3:

There is no single correct answer to this question, but Swift's system of controlling access suggests that it is not the best practice.

Properties and methods marked private are only visible within the same source file. If you put all of your model classes into a single file, they will all have access to each others' private methods and properties, which defeats the whole purpose of marking something private.



标签: swift class