简体   繁体   English

什么是receiveData的正确签名:fromPeer:inSession:context:in swift?

[英]Whats the correct signature of receiveData:fromPeer:inSession:context: in swift?

im trying to rewrite my GameKit multiplayer game (local) in swift and have problems with some missing documentation for the language. 即时通讯尝试在swift中重写我的GameKit多人游戏(本地),并且遇到一些缺少该语言文档的问题。 I want to receive data from another peer so i set the dataReceiveHandler for my GKSession like this: 我想从另一个对等体接收数据,所以我为我的GKSession设置了dataReceiveHandler,如下所示:

session.setDataReceiveHandler(self, withContext: nil)

in apples documentation it says that the dataReceiveHandler hast to implement a method with this signature: 在apples文档中,它说dataReceiveHandler有一个带有这个签名的方法:

SEL = -receiveData:fromPeer:inSession:context:

In the objective-c documentation is an example of how the signature should look like: 在objective-c文档中是签名应如何显示的示例:

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;

if i try to rewrite this method in swift it looks like this: 如果我尝试在swift中重写此方法,它看起来像这样:

func receiveData(data: NSData, fromPeer peer: String, inSession session: GKSession, withContext context: CMutableVoidPointer) {
    println("received data: \(data)")
}

and gives me this error when i receive a message: 并在收到消息时给我这个错误:

Domain=com.apple.gamekit.GKSessionErrorDomain Code=30500 "Invalid parameter for -setDataReceiveHandler:withContext:" UserInfo=0x178462840 {NSLocalizedDescription=Invalid parameter for -setDataReceiveHandler:withContext:, NSLocalizedFailureReason=The handler does not respond to the correct selector.} Domain = com.apple.gamekit.GKSessionErrorDomain Code = 30500“-setDataReceiveHandler的参数无效:withContext:”UserInfo = 0x178462840 {NSLocalizedDescription = -setDataReceiveHandler的无效参数:withContext:,NSLocalizedFailureReason =处理程序不响应正确的选择器。}

this means my method has not the correct signature. 这意味着我的方法没有正确的签名。 But what is the correct one? 但是正确的是什么?

It's a problem with renaming your parameters. 重命名参数是个问题。 You wrote withContext context instead of context withContext . withContext context写入withContext context而不是context withContext The first name is the one that will be exposed to callers. 第一个名称是将向呼叫者公开的名称。

This should work: 这应该工作:

func receiveData(data: NSData, fromPeer peer: String, inSession session: GKSession, context: CMutableVoidPointer) {
    println("received data: \(data)")
}

Sorry that i have to edit this again but i have the actual answer now: 对不起,我必须再次编辑,但我现在有了实际答案:

DONT USE GKSESSION! 不要使用GKSESSION! IT IS DEPRECATED SINCE iOS7! 自iOS7以来它已被弃用!

Use: 采用:

MultipeerConnectivity.framework MultipeerConnectivity.framework

You probably need to declare the types of the parameters as optionals (not sure whether this applies to the void pointer), ie func receiveData(data: NSData?, fromPeer peer: String?, inSession session: GKSession?, withContext context: CMutableVoidPointer) (you could also use ! instead of ? for implicitly unwrapped optionals). 您可能需要将参数的类型声明为可选项(不确定这是否适用于void指针),即func receiveData(data: NSData?, fromPeer peer: String?, inSession session: GKSession?, withContext context: CMutableVoidPointer) (您也可以使用!而不是?用于隐式解包的选项)。

Those parameters are pointers in Objective-C, ie they could be nil , which your current code cannot represent, thus it is does not have the correct signature. 这些参数是Objective-C中的指针,即它们可能是nil ,您当前的代码无法表示,因此它没有正确的签名。 The way to represent possible nil values in Swift is to use optionals. 在Swift中表示可能的nil值的方法是使用optionals。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM