How to pack struct in NSData? [duplicate]

≯℡__Kan透↙ 提交于 2019-12-09 06:05:34

问题


Possible Duplicate:
Send and receive NSData via GameKit

I have struct which consists of int variable and 2 float pointers (arrays). How can I pack this struct ib NSData and later unpack it?


回答1:


You can pack the structure using dataWithBytes method pf NSData :

struct aStruct {
/* Implementation */
};

//Struct variable 
aStruct exampleStruct;

// pack the struct into an NSData Object
NSData *myData = [NSData dataWithBytes:&exampleStruct length:sizeof(exampleStruct)];

// get back the the struct from the object
[myData getBytes:&exampleStruct length:sizeof(exampleStruct)];


来源:https://stackoverflow.com/questions/12328582/how-to-pack-struct-in-nsdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!