1
votes

In my iPhone app, I have to use MQTT's C based static library to send and receive its payload, the payload must be void *, I need to send messages such as text, picture and voice This is my sending method, I warp all kind of payloads into NSData object. And the message is a struct which have the void *payload

`+ (int)send:(NSString *)mqTopic payLoad:(NSData *)mqPayLoad`

MQTTClient_message pubmsg = MQTTClient_message_initializer;
pubmsg.payloadlen = mqPayLoad;

How can I fix the type conflict between C and ojbc?

Any help should be appreciated!

1

1 Answers

1
votes

NSData has a method - (const void *)bytes. So this should help resolve the type conflict you are facing.

NSUInteger len = [mqPayLoad length];
void *typedData = malloc(len);
memcpy(typedData, [mqPayLoad bytes], len);
pubmsg.payloadlen = typedData;

Since bytes returns a const void* its better to use a memcopy() rather than a typecast.