1

I want to build a service object with only static methods. Should I use struct or class or enum for this case? What the difference will be? May be it affects compile time? May be it affects speed somehow? I know structs are faster for instances. But how it affects this case?

Your thoughts? You experience?

Thank you.

struct/class/enum Linker {

    public static func skypeCall(contact:String) {
        //...
    }

    public static func phoneCall(phoneNumber:String) {
        //...
    }

    public static func openWebPage(url:String) {
       //... 
    }
}
Alex Shubin
  • 3,549
  • 1
  • 27
  • 32
  • 1
    Or enum, compare [Swift constants: Struct or Enum](https://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum) – which applies to static methods as well. – Martin R May 30 '17 at 14:14
  • @JAL Actually, this can be based on fact. There is a very limited set of considerations. You can objectively compare the differences in the choices. – rmaddy May 30 '17 at 14:28
  • @JAL, so you want to say, there's no facts does it affects performance or not? – Alex Shubin May 30 '17 at 14:36

1 Answers1

5

There are two key difference between a class and a struct in Swift. A class allows for inheritance and a class is a reference type while a struct is a value type.

Make your decision based on those few differences.

Since everything in Linker is static, the difference between reference and value becomes irrelevant since you won't have any instances.

So that leaves inheritance. Will you ever subclass Linker? If not, use a struct. If yes, use a class.

And now that you are asking about enum, you can probably rule that out over struct since Linker doesn't appear to have any constants, just methods.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thank you for an answer. I thought so. I just wondered does this decision can affect more than just convenience, code style, inheritance, autocomplete variants etc... – Alex Shubin May 30 '17 at 14:26