如何将NSValue转换为NSData并返回?

编程入门 行业动态 更新时间:2024-10-21 16:06:48
本文介绍了如何将NSValue转换为NSData并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

A有一些NSValue(通过KVC valueForKey获取),我需要将其附加到NSData对象,以便使用Game Center通过网络发送它.显然,我还需要将NSValue从NSData转换回更多的KVC(setValue:forKey:).

A have a number of NSValue (obtained via KVC valueForKey) that I need to append to an NSData object in order to send it over the network using Game Center. Obviously I will also need to convert the NSValue back from NSData for more KVC (setValue:forKey:).

我不知道每个NSValue的确切类型,所以我仅限于使用NSValue和NSData提供的接口.我应该提到NSValue绝不是指针类型.

I don't know the exact type of each NSValue, so I'm limited to using the interfaces provided by NSValue and NSData. I should mention that the NSValue are never pointer types.

我很惊讶没有[NSData dataWithValue:value],也没有类似[value dataWithEncoding:]之类的东西.但是也许我是盲人,没有看到明显的选择.我曾考虑过使用getValue:将值复制到void*缓冲区中,但是除了使用objCType并将其与所有可能的类型进行比较之外,我应该如何确定缓冲区的长度?

I'm surprised that there's no [NSData dataWithValue:value] and neither something like [value dataWithEncoding:] or similar. But maybe I'm blind and not seeing the obvious choice. I thought about using getValue: to copy the value into a void* buffer, but how am I supposed to determine the buffer length other than by using objCType and comparing that with all possible types?

我应该怎么做?

注意: NSKeyedArchiver毫无疑问,因为它效率极低.将4字节的浮点型归档为142字节的NSData,将8字节的CGPoint归档为NSData时使用260字节.保持发送的数据量最少对我正在做的事情至关重要.

NOTE: NSKeyedArchiver is out of the question because it is terribly inefficient. A 4 Byte float is archived to a 142 Bytes NSData, a 8 Byte CGPoint uses 260 Bytes when archived to NSData. Keeping the amount of data sent to a minimum is crucial to what I'm doing.

推荐答案

Martin Gordon的答案越来越近了,但是幸运的是,您不必手动解析objCType字符串.有一个功能可以做到: NSGetSizeAndAlignment .由此,您可以获得从getValue:获得的值的大小/长度. 这个问题引导我解决问题.

Martin Gordon's answer is getting close, but fortunately you don't have to manually parse the objCType string. There's a function that does that: NSGetSizeAndAlignment. From that you get the size/length of the value obtained from getValue:. This question lead me to the solution.

我最终为NSData创建了一个类别,该类别使我可以从NSNumber或NSValue对象创建NSData:

I ended up creating a category for NSData that allows me to create NSData from NSNumber or NSValue objects:

@interface NSData (GameKitCategory) +(NSData*) dataWithValue:(NSValue*)value; +(NSData*) dataWithNumber:(NSNumber*)number; @end @implementation NSData (GameKitCategory) +(NSData*) dataWithValue:(NSValue*)value { NSUInteger size; const char* encoding = [value objCType]; NSGetSizeAndAlignment(encoding, &size, NULL); void* ptr = malloc(size); [value getValue:ptr]; NSData* data = [NSData dataWithBytes:ptr length:size]; free(ptr); return data; } +(NSData*) dataWithNumber:(NSNumber*)number { return [NSData dataWithValue:(NSValue*)number]; } @end

我还在此NSValue/NSNumber数据之前添加了一个小标头,该标头使我可以解码该数据所针对的属性以及数据部分中的字节数.这样,我可以将值恢复到remote属性.

I also add a small header before this NSValue/NSNumber data that allows me to decode which property the data is for and how many bytes are in the data section. With that I can restore the value to the remote property.

更多推荐

如何将NSValue转换为NSData并返回?

本文发布于:2023-06-08 21:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   NSValue   NSData

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!