简体   繁体   English

将NSDictionary压缩为NSData的有效方法

[英]Efficient way of compressing NSDictionary into NSData

I am using BTLE to write data into one of the characteristics of peripheral. 我正在使用BTLE将数据写入外设的特征之一。 I wanted to send an NSDictionary into that characteristics. 我想向该特性发送一个NSDictionary Since there is a limitation of 130 bytes of data being sent over BTLE, I want an efficient way of compressing NSDictionary into NSData and then send across. 由于通过BTLE发送数据的限制为130字节,因此我需要一种将NSDictionary压缩为NSData然后进行发送的有效方法。 I am using below piece of code which is exceeding limit. 我正在使用以下超出限制的代码。 Any ideas? 有任何想法吗?

NSDictionary *aDict = @{ @"Value1": @"sadsadasdasdsadqwwqsadasd",
                         @"Value2": @"10",
                         @"Value3": @"12" };
NSData *aData = [NSKeyedArchiver archivedDataWithRootObject:aDict];
NSLog(@"Data Size = %@",
        [NSByteCountFormatter stringFromByteCount:aData.length
                                       countStyle:NSByteCountFormatterCountStyleFile]);

I don't think trying to use any form of compression will be effective, or even an improvement at all at this scale, because all compression algorithms work best when they have a lot of data to work with, and hence many duplicates and patterns to find. 我不认为试图使用任何形式的压缩将是有效的,甚至是改善所有在这个规模,因为所有的压缩算法效果最好时,他们有大量的数据的工作,因此很多重复和模式,找。 When your entire data size is 130 bytes, any form of zip compression isn't really a viable option. 当您的整个数据大小为130字节时,任何形式的zip压缩都不是切实可行的选择。


If your dictionary will only contain property-list values (arrays, dictionaries, strings, numbers), then you can use JSON serialisation instead of NSKeyedArchiver : 如果您的字典仅包含属性列表值(数组,字典,字符串,数字),则可以使用JSON序列化代替NSKeyedArchiver

NSData *JSONData = [NSJSONSerialization dataWithJSONObject:anObject
                                                   options:0
                                                     error:nil];

This immediately makes the output data much shorter in your case: 在您的情况下,这立即使输出数据短得多:

NSDictionary *aDict = @{ @"Value1": @"sadsadasdasdsadqwwqsadasd",
                         @"Value2": @"10",
                         @"Value3": @"12" };

NSData *aData = [NSKeyedArchiver archivedDataWithRootObject:aDict];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:aDict
                                                   options:0
                                                     error:nil];

NSLog(@"NSKeyedArchiver Data Size = %@, JSON Data Size = %@",
      [NSByteCountFormatter stringFromByteCount:aData.length
                                     countStyle:NSByteCountFormatterCountStyleFile],
      [NSByteCountFormatter stringFromByteCount:jsonData.length
                                     countStyle:NSByteCountFormatterCountStyleFile]
      );

NSKeyedArchiver Data Size = 380 bytes , JSON Data Size = 66 bytes NSKeyedArchiver数据大小= 380字节 ,JSON数据大小= 66字节

As you can see, the JSON serialised data is almost 6 times smaller than the NSKeyedArchiver serialised data, and fits easily in your 130 byte limit. 如您所见,JSON序列化数据几乎NSKeyedArchiver序列化数据的6倍 ,并且很容易满足您的130个字节的限制。 And the best thing is, it's only one line of code. 最好的是,它只是一行代码。

UPDATE : Just to rub it in some more :), here is the data that NSKeyedArchiver produces (added as image because it contains a lot of "illegal" characters that I couldn't copy and paste): 更新 :只是为了更进一步:),这是NSKeyedArchiver生成的数据(作为图像添加,因为它包含许多我无法复制和粘贴的“非法”字符):

NSKeyedArchiver数据

As you can see, it contains a lot of useless data that you don't really need (highlighted blue), that's basically just to give NSKeyedUnarchiver enough information to be able to unarchive it later. 如您所见,它包含许多您不需要的无用数据(突出显示为蓝色),基本上只是为NSKeyedUnarchiver足够的信息,以便以后可以对其进行归档。

Now, let's look at the JSON data: 现在,让我们看一下JSON数据:

{"Value3":"12","Value2":"10","Value1":"sadsadasdasdsadqwwqsadasd"}

That's it. 而已。 One line. 一条线。 66 bytes. 66个字节 Of those, 19 bytes aren't your values. 其中有19个字节不是您的值。 In other words, 71% of that JSON data is your values, and the rest is markup, so to speak. 换句话说,可以说该JSON数据中的71%是您的值,其余是标记。 Meanwhile, in the NSKeyedArchiver data, your values make up, wait for it, 12% of the result. 同时,在NSKeyedArchiver数据中,您的值组成了(等待)结果的12% I think you can clearly see which one is more efficient for storage here. 我认为您可以清楚地看到哪一种存储效率更高。

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

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