I would usually tend to use type methods with the appropriate type to give you the context of helper methods and not pollute the global namespace.
In Swift, structures are an ideal candidate for this kind of structure:
struct Helper { static func helpfulMethod() { ... } }
I also use inline structures with static constants quite liberally in my top level types to group related constants inside my code.
When writing custom Swift types, you should first consider using structures and only use classes when inheritance, reference semantics (as opposed to implicit copying with value semantics) or property semantics ( unowned / weak ) are needed. In this case, your utility functions will be void and there is no inheritance, so the structure should be preferable to the class.
I would say that, in general, Swift is moving away from global functions in favor of the implicit namespace provided by types (and protocols / generics). But it still depends heavily on style / personal preferences, and for something simple, like a utility function, that means little.
source share