是否可以在一个.swift文件中创建多个类,还是应该为每个类创建一个单独的.swift文件? [英] Is it acceptable to create multiple classes in one .swift file, or should I create a separate .swift file for each class?

查看:100
本文介绍了是否可以在一个.swift文件中创建多个类,还是应该为每个类创建一个单独的.swift文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 swift 文件中创建多个类是否被认为是不好的技术,还是最好创建一个单独的 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?

例如,哪个更好:

1。 alpha.swift

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



B 。每个类分别使用.swift文件。





1。在alpha.swift 中:

class alpha: UIViewController { ... }

2。在beta.swift 中:

class beta: UIWebView { ... }

3。在gamma.swift 中:

class gamma: UINavigationController { ... }


推荐答案

简短答案:这取决于

长答案:

如果您有小的 short 小类被牢固地绑定在一起,则将它们放在同一文件中是正确的。

如果您有很长的,不相关的类,那么最好将它们放在不同的文件中。

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.

这篇关于是否可以在一个.swift文件中创建多个类,还是应该为每个类创建一个单独的.swift文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆