0

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:

Error in Swift class: Property not initialized at super.init call - How to initialize properties which need use of self in their initializer parameter

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.

Community
  • 1
  • 1
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • Declaring client as implicitly unwrapped optional (as explained in the answer to the referenced question) should solve the problem. – Martin R May 10 '15 at 14:39
  • ah yes indeed, I mis-read the answer the first time around. I'll close this question. – MonkeyBonkey May 10 '15 at 14:46

0 Answers0