Swift 2:PubNub接收处理程序

时间:2016-07-17 15:39:20

标签: ios swift2 pubnub

我使用PubNub Swift 2.0代码接收消息如下。

R               = 500; % hexagol radius
N               = 50;  % number pair positions
d_min           = 10;  % minimum distance          
d_max           = 100; % maximum distance       
D               = (d_max - d_min).*rand(N,1) + d_min; % randomly distance
X               = [0,0]; % hexagol center
j=0;
while j < N 
    j=j+1;
    theta(j)=2*pi*rand(1,1);
    u= rand()+ rand();
    if u < 1
       r(j) = R * u;
    else
       r(j) = R * (2 - u);
    end
% to create the first position
    x1(j)=r(j)*cos(theta(j)) + X(1,1); % first x positions
    y1(j)=r(j)*sin(theta(j)) + X(1,2); % first y positions
end
% to create the second position
x2(j) = x1(j) + D(j); % second x positions
y2(j) = y1(j) + D(j); % second y positions

但是我在接收消息时需要处理额外的参数(myId),所以我将它添加到处理程序中以获得以下内容。

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)
    })
}

这在'handler(message:chatMessage ....'行''使用未解析的标识符myId'时失败。

无论我尝试修改什么,总会有错误。删除参数会导致错误,将“myId”添加到withBlock会失败。我想,因为CMD点击错误行'handler'会把我带到func行'handler',这意味着我必须为每个都有相同的参数。

我是否遗漏了某些内容,甚至尝试进行PubNub不允许的更改?

感谢。

0 个答案:

没有答案