I have working PubNub Swift 2.0 code for receiving messages as follows.
func receive(handler: (message: Message) -> ()) {
pubNub.observationCenter.removeMessageReceiveObserver(self)
pubNub.observationCenter.addMessageReceiveObserver(self, withBlock: { (message) -> Void in
self.logMessage("Received message: \(message.description)")
let dictionary: NSDictionary = message.message as! NSDictionary
let chatMessage = Message.deserialize(dictionary)
handler(message: chatMessage)
})
}
However I need to deal with an extra parameter (myId) when receiving a message so I added it to the handler to get the following.
func receive(handler: (message: Message, myId: UInt) -> ()) {
pubNub.observationCenter.removeMessageReceiveObserver(self)
pubNub.observationCenter.addMessageReceiveObserver(self, withBlock: { (message) -> Void in
self.logMessage("Received message: \(message.description)")
let dictionary: NSDictionary = message.message as! NSDictionary
let chatMessage = Message.deserialize(dictionary)
handler(message: chatMessage, myId: myId)
})
}
This fails at the 'handler(message: chatMessage....' line with 'use of unresolved identifier myId'.
No matter what I try to amend there is always an error. Removing parameters results in errors, adding 'myId' to the withBlock fails. I thought since CMD-clicking the error line 'handler' takes me to the func line 'handler', that meant I just have to have the same parameters in there for each.
Am I missing something or even trying to make changes that aren't allowed by PubNub?
Thanks.