解析NSData对象以获取信息 [英] parsing NSData object for information

查看:146
本文介绍了解析NSData对象以获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从我的服务器返回的 NSData 对象,它的内容不尽相同但坚持特定的结构。

I have a NSData object coming back from my server, it varies in its content but sticks to a particular structure.

我想知道(跳过一些示例代码)如何通过这个对象来获取我需要的数据。

I would like to know (hopfully with some example code) how to work though this object to get the data I need out of it.

对象内部数据对象的结构如下所示

the structure of the data objects inside the objects are like this


  • 领先值( UInt16 ) - (告诉我它的响应部分)

  • 字符串的大小( UInt32 数字 - ( UInt32

  • 字符串(非空终止),即后跟前导值。

  • leading value (UInt16) - (tells me what section of the response it is)
  • Size of string (UInt32) or number - (UInt32)
  • String (not null terminated) i.e. followed by the next leading value.

我一直在阅读二进制数据编程指南然而,这只是真正告诉我如何将我的数据放入新的 NSData 对象以及访问和计算字节数。

I have been reading through the Binary Data Programming Guide however that's only really showing me how to put my data into new NSData objects and accessing and compairing the bytes.

我坚持的是如何动态抓取信息。检查 NSdata 对象的第一个前导值,如果它的字符串或int,则获取字符串或int并移动到下一个前导值..

The thing I am stuck on is how do I say grab the info dynamically. Check the NSdata objects first leading value figure out if its string or int then get the string or int and move onto the next leading value..

任何建议或示例代码都会非常有用..因为我从未在目标C中尝试过这样的事情,所以只是坚持了一个心灵障碍。

any suggestions or example code would be really helpfull.. just stuck in abit of a mind block as I have never attempted anything like this in objective C.

推荐答案

其中一些取决于服务器的编写方式,将数据编码为发送给您的数据。假设它使用标准网络字节顺序(big-endian)对数值进行编码,你会希望它转换为iOS的正确字节顺序(我相信它总是小端)。

Some of this depends on how your server is written to encode the data into what it is sending you. Assuming it is encoding the numeric values using standard network byte ordering (big-endian) you will want it converted to the correct byte-ordering for iOS (I believe that is always little-endian).

我会接近这样的事情:

uint16_t typeWithNetworkOrdering, typeWithLocalOrdering;
uint32_t sizeWithNetworkOrdering, sizeWithLocalOrdering;
char *cstring = NULL;
uint32_t numberWithNetworkOrdering, numberWithLocalOrdering;
const void *bytes = [myData bytes];
NSUInteger length = [myData length];

while (length > 0) {
    memcpy(&typeWithNetworkOrdering, bytes, sizeof(uint16_t));
    bytes += sizeof(uint16_t);
    length -= sizeof(uint16_t);
    memcpy(&sizeWithNetworkOrdering, bytes, sizeof(uint32_t));
    bytes += sizeof(uint32_t);
    length -= sizeof(uint32_t);
    typeWithLocalOrdering = CFSwapInt16BigToHost(typeWithNetworkOrdering);
    sizeWithLocalOrdering = CFSwapInt32BigToHost(sizeWithNetworkOrdering);

    if (typeWithLocalOrdering == STRING_TYPE) { // STRING_TYPE is whatever type value corresponds to a string
        cstring = (char *) malloc(sizeWithLocalOrdering + 1);
        strncpy(cstring, bytes, sizeWithLocalOrdering);
        cstring[sizeWithLocalOrdering] = '\0';
        NSString *resultString = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
        NSLog(@"String = %@", resultString);
        free(cstring);
        bytes += sizeWithLocalOrdering;
        length -= sizeWithLocalOrdering;
        // Do whatever you need to with the string
    }
    else if (typeWithLocalOrdering == NUMBER_TYPE) { // NUMBER_TYPE is whatever type value corresponds to a number
        memcpy(&numberWithNetworkOrdering, bytes, sizeof(uint32_t));
        numberWithLocalOrdering = CFSwapInt32BigToHost(numberWithNetworkOrdering);
        NSLog(@"Number = %u", numberWithLocalOrdering);
        bytes += sizeof(uint32_t);
        length -= sizeof(uint32_t);
        // Do whatever you need to with the number
    }
}

这篇关于解析NSData对象以获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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