I'm trying to create a chat service singleton in swift and getting some errors initializing properties. If I do it this way I get the error self.client not initialized at super.init call
class ChatService : NSObject, PTPusherDelegate {
var client:PTPusher
static let sharedInstance = ChatService()
override init() {
super.init()
self.client = PTPusher.pusherWithKey("test", delegate: self) as! PTPusher
client.connect()
}
}
However if I move it before the super.init call I get self used before super.init call
override init() {
self.client = PTPusher.pusherWithKey("test", delegate: self) as! PTPusher
super.init()
client.connect()
}
I can declare var client:PTPusher? as optional, but it's really not optional and don't want to always append an exclamation mark to client!
What's the recommended way of doing this? I saw this stack overflow question:
That seems to address the same topic and it suggests to invert the order of initializing properties like in my second example, but I still get an error.