Reputation: 8386
I have a singleton class:
final class NotificationSingleton : NSObject {
static let sharedInstance = NotificationSingleton()
var aProperty: String!
var anotherProperty: Int!
}
How can I make sure that aProperty
and anotherProperty
are only accessible through the sharedInstance
?
Upvotes: 0
Views: 36
Reputation: 17958
You could use failable initializers to prevent the creation of additional NotificationSingleton
instances once your sharedInstance
has been set.
I suspect this sort of defensive programming will actually make the class hostile to use by other developers in the future, difficult to test, and that there's probably a cleaner solution available which doesn't rely on singletons as globals but there you go.
Upvotes: 0
Reputation: 11868
you can make the initializer private thus no other instances can be created
private override init() {}
Upvotes: 2