0

Coding in Swift right now. In terms of performance and memory usage, which way is superior? Any other things to take into consideration? Thanks!

Edit: I have read this Difference between static function and singleton class in swift but I still do not understand the difference. Is there a definitive rationale for the extra complexity of a singleton?

1) the regular singleton

class MyClass {
    private init() {}
    static let shared = MyClass()
    func myFunc() {...}
    var myVar = ...
}

2) the statics

struct MyStruct {
    private init() {}
    static func myFunc() {...}
    static var myVar = ...
}
Dennis Lau
  • 771
  • 1
  • 8
  • 10
  • Bottom line it really depends on what you are trying to achieve, the use case you need for the technology that exist for that language. – OhadM Dec 08 '18 at 18:29
  • 1
    https://stackoverflow.com/questions/37806982/difference-between-static-function-and-singleton-class-in-swift, https://stackoverflow.com/questions/36312461/singleton-vs-staticclass-variables – Martin R Dec 08 '18 at 18:31
  • Sorry I wasn't more clear, talking about iOS and Swift here – Dennis Lau Dec 08 '18 at 18:32
  • 1
    Possible duplicate of [Difference between static function and singleton class in swift](https://stackoverflow.com/questions/37806982/difference-between-static-function-and-singleton-class-in-swift) – OhadM Dec 08 '18 at 18:34
  • I did read the "Difference between static..." question, but then I do not understand the point of the Singleton pattern. "Utility class ... It should not deal with async task and expensive resource..." Why is this true? – Dennis Lau Dec 08 '18 at 18:39

0 Answers0