Chao Peng
Chao Peng

Reputation: 97

Is final necessary for a singleton class in Swift?

To create a singleton class, I wrote something like this:

class SingletonEx{
    var name = ""
    private init(){}

    static let sharedInstance = SingletonEx()

    func instanceMethod(){  
    }

    static func classMethod(){
    }
}

Some tutorials say final is necessary while others just ignore final keyword. After I tried subclassing SingletonEx, I got the following results.

enter image description here

enter image description here

enter image description here

It seems I can't write an initializer for subclass, which means I can't use an override instance method in a subclass.

As far as I know, singleton definition is all about single instantiation and accessing instance methods through the only instance. So I don't think it is necessary to use final in the singleton definition. But both my teachers and some online tutorials say it is necessary.

I got confused, since you can't create a subclass instance anyway, even you override the instance methods, you can't use it or access it, what's the point to say final is necessary for a singleton class?

If I am wrong, please point out.

Upvotes: 2

Views: 1121

Answers (1)

Mohammad Reza Koohkan
Mohammad Reza Koohkan

Reputation: 1734

Super Class

First of all you need to know the properties and methods that are marked with private are just known to the Super class and Sub classes won't access them!

A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Inheritance is a fundamental behavior that differentiates classes from other types in Swift.

Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior. Swift helps to ensure your overrides are correct by checking that the override definition has a matching superclass definition.

In your case in SingletonEx class you market init with private which means that you can create object just in the body of the class! that means no one, no where, can't create an object of SingletonEx!

If you want to a method end up in Super Class and you don't want to Sub classes overide that method you need to mark that method with final which means it's not private but its available only from Super class!


Sub Class

When class Y Inheritance from SingletonEx which means that cant create an object outside of the class ! because Super class initializer is unavailable during init() method from class Y ! While you need to call the super.init() if you want to initialize an object from Y class !

if you remove private from private init() {} from SingletonEx class you be able to create object from SingletonEx class and also from Y class !


your code should looks like this :

Swift 4 :

class SingletonEx{

   var name = ""

   init(){}

   static let sharedInstance = SingletonEx()

   func instanceMethod(){
   }

   static func classMethod(){
   }
}




class Y : SingletonEx {

   private var yName = "Y name is : "

   init(name:String) {
      super.init()
      self.name =  self.yName + name
   }

}

Usage :

override func viewDidLoad() {
    super.viewDidLoad()

    let yObject = Y.init(name: "badGirl :D ")

    print(yObject)

    // --> Output : Y name is : badGirl :D   
}

Upvotes: 1

Related Questions