Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store extensions in a Swift project

Tags:

swift

swift2

For example, I want to use this extension:

import Foundation

extension String {
    func exec (str: String) -> Array<String> {
       ....
    }
}

Where should I save it? Should I create a new file extensions.swift?

like image 773
Arti Avatar asked Jan 07 '23 07:01

Arti


1 Answers

For global extensions such as the one above I think it would be best to put it in an extensions.swift file. If your extending one of your own classes I find it best to keep it in the same file as the original class so that other developers know about it.

If you have multiple extensions for a particular global class such as String, you could group them into a StringExtensions.swift file.

like image 80
Scriptable Avatar answered Jan 14 '23 21:01

Scriptable