使用 ARC 通过结构发送玩家别名(游戏中心)
由于 ARC/指针,我无法将 NSString 放入我的结构中,因此将本地玩家的别名(或与此相关的任何文本)发送到另一个设备的最佳方法是什么?
到目前为止,我已经尝试转换为 &从 char 数组,使用 __unsafe_unretained- 选项并尝试创建一个类来放入文本。虽然所有这三个尝试都通过编译完成,但它们使设备崩溃(模拟器继续运行,但没有别名显示。)
正在发送文本使用ARC进行多人游戏真的很难吗?我面临的问题很可能是由于我在编程方面经验不足......所以如果有人能指出我正确的方向或为我提供一些代码片段,我将非常感激。
What is the best approach for sending the local player's alias (or any text for that matter) to another device, since I can't put an NSString in my struct because of ARC/pointers?
So far, I've tried converting to & from a char array, using the __unsafe_unretained- option and trying to create a class to put the text in. Though all three of these attempts worked through compiling, they crashed the device (simulator keeps running but no alias displays.)
Is sending text in multiplayer games really difficult when using ARC? The issues I'm facing are most likely a result of the fact that I am not very experienced at programming... so if anyone could point me in the right direction or provide me with some snips of code, I'd really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松地将字符串编码和解码为 NSData 对象,并通过 Game Center 发送它们。
编码:在字符串上使用此方法
编码:NSUTF8StringEncoding
这将返回一个 NSData 对象
解码;
编辑:
本质上你永远不需要显式发送玩家别名。有两种情况:
1:GKTurnBasedMatch
如果您使用这个,那么这里是如何获取所有别名(包括您的)的列表
2.GKMatch :这种情况更容易,因为您的 GKMatch 已经有playerIDs数组,与前:
You can easily encode and decode strings to NSData objects and send them over Game Center.
To encode: use this method on a string
With encoding:NSUTF8StringEncoding
This will return a NSData object
To decode;
Edit:
Essentially you'll never have to send player aliases explicitly. There are 2 cases:
1: GKTurnBasedMatch
If you're using this, then here's how to get a list of all of the aliases (including yours)
2.GKMatch : this case is much easier since your GKMatch has already playerIDs array, same as before: