如何在 NSData 中打包结构体? [英] How to pack struct in NSData?

查看:61
本文介绍了如何在 NSData 中打包结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
通过 GameKit 发送和接收 NSData

我有一个由 int 变量和 2 个浮点指针(数组)组成的结构.我如何打包这个 struct ib NSData 并稍后解包?

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?

推荐答案

可以使用 dataWithBytes 方法 pf NSData 打包结构:

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)];

这篇关于如何在 NSData 中打包结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆