简体   繁体   English

如何连接3个NSData变量

[英]How to concatenate 3 NSData variables

How to concatenate 3 NSData variables ? 如何连接3个NSData变量?

NSData *iv;
NSData *salt;
NSData *encryptedData;

I need to join these to a single variable. 我需要将这些加入一个变量。 Can any one show me a way. 任何人都可以告诉我一个方法。

use an NSMutableData object and the method -(void)appendData:(NSData *)otherData 使用NSMutableData对象和方法-(void)appendData:(NSData *)otherData

Edited to add example : 编辑添加示例:

NSMutableData *concatenatedData = [NSMutableData data];
[concatenatedData appendData:iv];
[concatenatedData appendData:salt];
[concatenatedData appendData:encryptedData];
// and now you have all of the data in the single variable "concatenatedData"

For those who coding for iOS5 and later. 适用于编写iOS5及更高版本的用户。 I'd like to show some real good concatenation. 我想展示一些真正好的连接。 Why are those answers aren't good enough? 为什么这些答案不够好? Because they are involves extra memory usage for copied data. 因为它们涉及复制数据的额外内存使用。 Let's see the answer: 让我们看看答案:

NSMutableData *concatenatedData = [NSMutableData data];
[concatenatedData appendData:iv];
[concatenatedData appendData:salt];
[concatenatedData appendData:encryptedData];

here we have memory allocated for iv , salt and encryptedData also each time we append one of them to our mutable concatenation we are obviously copy it to mutable data again. 这里我们为ivsaltencryptedData分配了内存,每次我们将其中一个附加到我们的可变连接中时,我们显然会再次将其复制到可变数据中。 Do we want this extra expenses while dealing with large data? 在处理大数据时,我们是否需要这些额外费用? Me not. 我不是。

There is a way to avoid this unnecessary expense of memory - dispatch_data I'm not going to explain how it works, you can google it if you want. 有一种方法可以避免这种不必要的内存消耗 - dispatch_data我不打算解释它是如何工作的,你可以根据需要谷歌。 I just give you a code that works: 我只是给你一个有效的代码:

NSData *iv = [@"some data" dataUsingEncoding:NSUTF8StringEncoding];
NSData *salt = [@"even more data" dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [@"and one more" dataUsingEncoding:NSUTF8StringEncoding];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_data_t dispatch_data_iv = dispatch_data_create([iv bytes], [iv length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t dispatch_data_salt = dispatch_data_create([salt bytes], [salt length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t dispatch_data_encrypted = dispatch_data_create([encryptedData bytes], [encryptedData length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
iv = nil; salt = nil; encryptedData = nil; // free all parts, we dont need it anymore
dispatch_data_t dispatch_data_concat = dispatch_data_create_concat( dispatch_data_create_concat(dispatch_data_iv, dispatch_data_salt), dispatch_data_encrypted);
NSData *concatenatedNSData = DataFromDispatchData(dispatch_data_concat);

// lets check now if the concatenation works properly
NSString *stringFromConcatenatedNSData = [[NSString alloc]initWithData:concatenatedNSData encoding:NSUTF8StringEncoding];
NSLog(@"%@",stringFromConcatenatedNSData); 

don't forget about the helper-converter 不要忘记辅助转换器

NSData *DataFromDispatchData(dispatch_data_t data)
{
    NSMutableData *result = [NSMutableData dataWithCapacity: dispatch_data_get_size(data)];
    dispatch_data_apply(data, ^(dispatch_data_t region, size_t offset, const void *buffer, size_t size) {
        [result appendBytes:buffer length:size];
        return (_Bool)true;
    });
    return result;
}

You could use NSMutableData 's -appendData method: 您可以使用NSMutableData-appendData方法:

NSMutableData *result = [NSMutableData data];

[result appendData:iv];
[result appendData:salt];
[result appendData:encryptedData];

// result now has what you need.

This comes at the overhead of using mutable data, which can be slower & use more memory, so use with care. 这是在使用可变数据的开销,这可能会更慢并使用更多内存,因此请小心使用。 Generally speaking you don't want large NSData 's floating around. 一般来说,你不希望大型NSData浮动。

First create two NSObjects and use this method 首先创建两个NSObjects并使用此方法

-(void)appendData:(NSData *)otherData  

and put in one NSData later with 3rd NSData also concatenate with the same method. 然后在第三个NSData放入一个NSData也用同一个方法连接。

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

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