Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Overlaps?

I have two classes using as singletons.

class Boss {
    static let sharedInstance = Boss()
    private init() {} 

    var user_id : String?
    var username : String?
}



class Job {
    static let sharedInstance = Job()
    private init() {} 

    var job_id : String?
    var JobType : String?
}

But after populating the Boss.sharedInstance first, Job.sharedInstance also containing Boss' class variables. But after replacing the sharedInstance with other name (for eg. job_sharedInstance and boss_sharedInstance) respectively, things are working fine. It's quite weird. Can anybody explain me the reason why it would happen like this. Thanks in advance.

Here is the breakpoint. Although Job.sharedInstance does not have user_id, username, etc..., it's showing up.

enter image description here

like image 781
cloudy45man Avatar asked Jan 28 '26 03:01

cloudy45man


1 Answers

There is no problem with your code. Both Boss and Job have a static let sharedInstance property, and these are completely independent of each other. Different classes can have static properties with the same name, and these don't "overlap".

If the debugger shows properties for Job.sharedInstance which are not even defined in the Job class then this is a bug in the debugger view.

When in doubt, add print statements to your code.

like image 124
Martin R Avatar answered Jan 29 '26 20:01

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!