I would like to know how to achieve this: Assume I have a singleton class as
class Global{
static let shared = Global()
private init(){}
}
I want this class as closed to modification. But open to extend.
I want to achieve result as
Global.shared.var1
When var1
is coming from another class somehow extending Global
.
It's a wish. Is it even possible? What is the right way to achieve this.
Found a hack that served my purpose for the time being (suggest me a better way/alternate):
class Students{
static let shared = Students()
private init(){}
var name: [String] = ["Farhan","Hasan","Saba","Fatima"]
}
class Teachers{
static let shared = Teachers()
private init(){}
var name: [String] = ["Mr. Riaz","Ms. Ayesha"]
}
//Base for Singleton, sort of proxy
class Global{
private init(){}
}
//Somewhere else in your project
extension Global{
static let students = Students.shared
}
//Somewhere else in your project
extension Global{
static let teachers = Teachers.shared
}
//Apparently it served the purpose
print(Global.students.name) //prints: ["Farhan", "Hasan", "Saba", "Fatima"]
print(Global.teachers.name) //prints: ["Mr. Riaz", "Ms. Ayesha"]