简体   繁体   English

Obj-C / iOS:如何检索NSData的内容并进行交互?

[英]Obj-C/iOS: How to retrieve contents of NSData and interact?

I'm retrieving a unix timestamp from a Bluetooth LE peripheral, which is stored in an NSData object. 我正在从存储在NSData对象中的Bluetooth LE外设检索unix时间戳。 If I print the contents of the NSData object to the debug console, they appear correct, however if I try to convert the NSData object to an integer value, the integer value appears to keep changing. 如果我将NSData对象的内容打印到调试控制台,则它们看起来正确,但是,如果我尝试将NSData对象转换为整数值,则该整数值似乎会不断变化。

NSData *refinedData = [mfrData subdataWithRange:range];

Which yields a value of 386d5e9a on the debug console. 在调试控制台上产生的值为386d5e9a。

I then convert to an integer: 然后,我将其转换为整数:

uint32_t unixTimeStamp = refinedData;

Initially, this yields a value of 342162144 on the debug console. 最初,这在调试控制台上产生的值为342162144。 However, this value keeps growing, despite the NSData not changing. 但是,尽管NSData不变,该值仍在增长。 Can anybody help me understand what's going on? 有人可以帮助我了解发生了什么吗?

If it's not already very apparent, I'm a newbie. 如果不是很明显,我是新手。

Thanks. 谢谢。

refinedData is a pointer to an instance of NSData. refinedData是指向NSData实例的指针。 You want to access its contents: 您要访问其内容:

uint32_t unixTimeStamp = *(uint32_t *)[refinedData bytes];

Note that this is simplified, and assumes that the bytes returned by the Bluetooth peripheral are the same endianness as the processor in your device, that range is correct, etc. 请注意,这是经过简化的操作,并假定蓝牙外设返回的字节的字节序与设备中的处理器的字节序相同, range正确等。

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

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