简体   繁体   English

目标 C 中的字符数组或在没有指针的结构中存储字符串(不使用 NSString - 我认为)

[英]Array of Char or Storing a string in a struct without a pointer (Not using NSString - I think) in objective C

I'm creating a game in objective C using the cocos 2d engine for iPhone.我正在使用 iPhone 的 cocos 2d 引擎在目标 C 中创建游戏。 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.它无法解开 NSString 并引发错误,可能是因为它不知道结构中该变量的大小/长度。

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, ie 32 characters), my question is....所以,因为,我真的不想修改我包装或解开我的数据包的方式(我正在将 NSMutableDictionary 转换为 JSON,一切都很好,但这似乎是倒退了一步)但我想允许发送一个字符串(显然固定的字符串长度/大小很好,即 32 个字符),我的问题是....

Is there an easy way to handle a fixed length string without a pointer in objective C, array of 32 chars or something similar?有没有一种简单的方法来处理没有指针的固定长度字符串目标 C、32 个字符的数组或类似的东西?

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...)如果是这样,我如何获取最大 32 char NSString 并将其应用于它们(我猜标题不相关......)

Thanks for you help,谢谢你的帮助,

JT捷通

You can use the - (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding method on NSString.您可以在 NSString 上使用- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding方法。 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:因此,您将添加一个具有固定长度的 char 数组到您的结构中,并使用上面的方法将 NSString 写入其中:

typedef struct
{
    PacketType type;
    char name[MAX_LENGTH];
} PacketHello

NSString* toSend = @"blahblahblah";
PacketHello myPacket;
[toSend getCString:myPacket.name maxLength:MAX_LENGTH encoding:NSUTF8StringEncoding];

On the recieving end you can convert it back to an NSString with - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding在接收端,您可以使用- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding将其转换回 NSString

PacketHello received;
NSString* name = [[NSString alloc] initWithCString:received.name encoding:NSUTF8StringEncoding];

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

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