Using a function or type method?

I want to create a Helper.swift file to add some functions / methods that are useful in different parts of my application.

I was wondering what is best (if any): create a class and only create type methods or just create functions?

+6
source share
6 answers

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.

+4
source

There is no need to create a class or structure. You can simply put functions directly into the Helper.swift file. And you don’t even need to import this file by creating import instructions.

To verify this, create a helper.swift file and simply add the following lines of code to the file. No class or structure required.

 import Foundation func addTwo(x: Int) { return x+2 } 

To use this function in any other file, just call the function with the necessary arguments.

 let a = addTwo(9) 

I prefer this method because when you call a function you do not need to call it on an instance of the class / structure. Secondly, this leads to cleaner code, since you do not need to make each function a class function or a static function.

+1
source

I prefer a type method, because in this way you can easily distinguish your methods.

Suppose you have 50 to 60 methods in which some methods are designed for development, some methods are for calculation purposes, and some methods are for receiving and publishing data on the server.

Now in this case, if you create all these methods as globally, it becomes difficult to recognize and remember.

Now, if you distinguish between these methods in some class / structure, as shown below:

  • The methods that are used for design purposes create the DesignHelper class class / struct and put all these methods into it as a class/static method
  • The methods that are used for calculation purposes make up a MathHelper class / struct and put the whole method into them as a class/static method
  • The methods used for the process data with the server form the ConnectionHelper class class / struct and put all these methods into it as a class/static method

Using this method, you can easily learn any of the methods, as well as help in auto completion .

Thanks.

+1
source

A class function works through one class, but usually those functions that are used throughout the application will be repeated on the same objects. A cleaner way is to define your functions in the extensions of the object that will use this function. You can put all your extensions in Helper.swift

eg.

 extension UIColor { class func randomColor() -> UIColor { var randomRed:CGFloat = CGFloat(drand48()) var randomGreen:CGFloat = CGFloat(drand48()) var randomBlue:CGFloat = CGFloat(drand48()) return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0) } } 

using as page.backgroundColor = UIColor.randomColor()

Thus, you can still define your functions as functions of a class or function of an object, depending on usage, but also within the scope of the extension of the object.

This allows you to clear the code, so you do not route your calls through an assistant throughout the code base. It is clearly defined for an object that will need a function. If you find code that does not make sense in an extended function, the function probably needs to be reorganized into more focused functionality.

+1
source

Solution No. 1:

 class Helper { class func classMethod() { // TODO: play something } } 

And call him:

 Helper.classMethod() 

In an Apple doc:

Structures are always copied when they are passed in your code, and do not use reference counting.

Solution No. 2:

 struct Helper { static func classMethod() { // TODO: do something } } 

And use it:

 Helper.classMethod() 

I think solution # 2 is better than solution # 1 because it does not increase reference counting.

UPDATED: I tested using the playground . See Result below:

enter image description here Hope this helps!

0
source

I don’t know if this is the best practice, but here is how I do it.

First I declare a protocol:

 protocol UtilityType { } 

Then I extend it (for example, a utility function for a UIViewController)

 extension UtilityType where Self: UIViewController { func setDefaultTitleAttributes() { navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] } } 

And I use it like this

 class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() setDefaultTitleAttributes() } } extension MyViewController: UtilityType {} 
0
source

All Articles