Should I keep extensions in their own “Extensions” file?

匆匆过客 提交于 2019-12-13 15:13:35

问题


I have a few extensions that I'd like to use throughout my project but I'm unsure if I should keep them in their own file, or if I should just make specific extensions in each viewController file.

Which would be more efficient or better?


回答1:


I think this is largely a matter of style rather than efficiency. That said, yes you should put them in their own file. That way if you want to reuse them between projects, you can simply drag and drop them into a new project.

In general, it is best to keep your files as small and modular as possible in order to keep things simple. If a piece of functionality doesn't specifically pertain to that file or viewcontroller then I think you should break it into its own file.




回答2:


I usually make files following the naming convention of Objective-C categories which are similar.

So, I put my String extensions in "String+Extensions.swift" and my UIViewController extensions in "UIViewController+Extensions.swift"

If I want to sub-categorize even further, then I make more files with different words after the +. In file names, to iOS devs that have done a lot of Objective-C, a + means "has categories" and for Swift files, means "has extensions".



来源:https://stackoverflow.com/questions/42841953/should-i-keep-extensions-in-their-own-extensions-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!