Cameron L
Cameron L

Reputation: 111

Should I keep extensions in their own "Extensions" file?

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?

Upvotes: 3

Views: 1562

Answers (2)

toddg
toddg

Reputation: 2906

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.

Upvotes: 4

Lou Franco
Lou Franco

Reputation: 89232

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".

Upvotes: 0

Related Questions