在 Objective C 中,Char 数组或在没有指针的结构中存储字符串(不使用 NSString - 我认为)
我正在使用 iPhone 的 cocos 2d 引擎在 Objective C 中创建一个游戏。目前我正在使用结构向客户端发送数据包或从客户端发送数据包:
typedef struct
{
PacketType type;
CGPoint position;
int amount;
int playerId;
} PacketPlayerDamage;
我的问题是我想以相同的格式发送一个字符串,否则我必须进行一些相当大的更改,理想情况下以下内容会起作用:
typedef struct
{
PacketType type;
NSString *name;
} PacketHello
我相信这不会当我打包数据包发送它时,我执行以下操作:
PacketPlayerDamage packet;
packet.type = kPacketTypePlayerDamage;
packet.amount = amount;
packet.playerId = playerId;
NSData* sendPacket = [NSData dataWithBytes:data length:sizeof(packet)];
[self sendToAll:sendPacket];
无法解开 NSString 并引发错误,可能是因为它不知道结构中该变量的大小/长度。
因此,从那时起,我真的不想修改我包装或解开数据包的方式(我将 NSMutableDictionary 转换为 JSON,一切都很好,但这似乎是倒退了一步),但我想允许发送字符串(显然固定的字符串长度/大小很好,即32个字符),我的问题是......
有没有一种简单的方法可以处理固定长度的字符串,而无需目标C中的指针,32个字符的数组或类似的东西?
我看到了以下内容:
UTF8String option1;
Str32 option2;
它们是我要找的吗?如果是这样,我该如何获取最大 32 个字符的 NSString 并将其应用于它们(我猜标题不那么相关......)
谢谢你的帮助,
JT
I'm creating a game in objective C using the cocos 2d engine for iPhone. Currently i'm sending packets of data to and from clients using structs:
typedef struct
{
PacketType type;
CGPoint position;
int amount;
int playerId;
} PacketPlayerDamage;
My problem is I want to send a string in the same format, otherwise I have to make some pretty major changes, ideally the following would work:
typedef struct
{
PacketType type;
NSString *name;
} PacketHello
I believe this doesn't work as when i'm wrapping up my packet to send it I do the following:
PacketPlayerDamage packet;
packet.type = kPacketTypePlayerDamage;
packet.amount = amount;
packet.playerId = playerId;
NSData* sendPacket = [NSData dataWithBytes:data length:sizeof(packet)];
[self sendToAll:sendPacket];
Which fails to unwrap a NSString and throws an error, possibly because it doesn't know the size/length of that variable in the struct.
So since, I don't really want to modify the way I wrap or unwrap my data packet (i was converting NSMutableDictionary's to JSON and everything was fine, but that seems like a step backwards) but I want to allow the sending of a string (obviously fixed string length/size is fine, i.e. 32 characters), my question is....
Is there an easy way to handle a fixed length string without a pointer in objective C, array of 32 chars or something similar?
I've seen the following:
UTF8String option1;
Str32 option2;
Are they what i'm looking for? If so, how do I take a max 32 char NSString and apply it to them (, and I guess the title isn't as relevant...)
Thanks for you help,
JT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 NSString 上使用 - (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding 方法。因此,您可以添加一个固定长度的 char 数组到您的结构中,并使用上面的方法将 NSString 写入其中:
在接收端,您可以使用 - (id)initWithCString:( 将其转换回 NSString:( const char *)nullTermminateCString 编码:(NSStringEncoding)编码
You can use the - (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding method on NSString. So you would add add a char array to your struct with a fixed length and use the method above to write the NSString into it:
On the recieving end you can convert it back to an NSString with - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding