版本:iOS13.5
data.h
索引
- 从给定的连续内存缓冲区中创建一个调度数据对象
dispatch_data_create
- 返回调度数据对象的内存区域的逻辑大小
dispatch_data_get_size
- 将调度数据对象data表示的内存映射为单个连续的内存区域buffer_ptr,并返回表示该内存区域的新数据对象
dispatch_data_create_map
- 返回一个表示data1和data2串联的新创建的数据对象
dispatch_data_create_concat
- 返回数据对象data某范围的新调度数据对象
dispatch_data_create_subrange
- 遍历调度数据对象data表示的内存区域,并为遇到的每个连续内存区域调用一次applier
dispatch_data_apply
- 在数据对象data表示的区域中查找指定位置location的连续内存区域,并返回表示该区域的调度数据对象的副本及其在对象data中的逻辑偏移量
dispatch_data_copy_region
详解
- 从给定的连续内存缓冲区中创建一个调度数据对象
dispatch_data_t dispatch_data_create(const void *buffer, size_t size,
dispatch_queue_t _Nullable queue,dispatch_block_t _Nullable destructor);
buffer 连续的数据缓冲区
size 连续的数据缓冲区的大小
queue 队列
destructor 该回调负责在不需要时释放数据
如果使用DISPATCH_DATA_DESTRUCTOR_DEFAULT
,则数据对象的创建会将缓冲区复制到系统管理的内部存储器中
如果使用DISPATCH_DATA_DESTRUCTOR_FREE
,系统将通过free(3)来释放缓冲区,并忽略queue
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
- 返回调度数据对象的内存区域的逻辑大小
size_t dispatch_data_get_size(dispatch_data_t data);
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
size_t size = dispatch_data_get_size(data);
NSLog(@"%ld %ld", size, 10*sizeof(char));
输出:
10 10
- 将调度数据对象data表示的内存映射为单个连续的内存区域buffer_ptr,并返回表示该内存区域的新数据对象
dispatch_data_t dispatch_data_create_map(dispatch_data_t data,
const void *_Nullable *_Nullable buffer_ptr, size_t *_Nullable size_ptr);
buffer_ptr 连续内存区域的指针,该指针变量将使用映射的连续内存区域的位置,可为NULL
size_ptr size_t变量的指针,指向要用映射的连续内存区域的大小,可为NULL
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
size_t size = dispatch_data_get_size(data);
const void *bytes = NULL;
dispatch_data_t data1 = dispatch_data_create_map(data, &bytes, &size);
NSMutableData *totalData = [NSMutableData dataWithBytes:bytes length:size];
NSString *string = [[NSString alloc] initWithData:totalData encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
输出:
1234567890
- 返回一个表示data1和data2串联的新创建的数据对象
dispatch_data_t dispatch_data_create_concat(dispatch_data_t data1, dispatch_data_t data2);
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data2 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t data3 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t data4 = dispatch_data_create_concat(data2, data3);
NSString *string = [[NSString alloc] initWithData:(NSData *)data4 encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
输出:
12345678901234567890
- 返回数据对象data某范围的新调度数据对象
dispatch_data_t dispatch_data_create_subrange(dispatch_data_t data,
size_t offset, size_t length);
offset 数据对象开始处的偏移量
length 范围的长度
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data5 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t data6 = dispatch_data_create_subrange(data5, 3, 4);
NSString *string = [[NSString alloc] initWithData:(NSData *)data6 encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
输出:
4567
- 遍历调度数据对象data表示的内存区域,并为遇到的每个连续内存区域调用一次applier
bool dispatch_data_apply(dispatch_data_t data, dispatch_data_applier_t applier);
bool 遍历是否成功完成
applier 要调用的回调
typedef bool (^dispatch_data_applier_t)(dispatch_data_t region,
size_t offset, const void *buffer, size_t size);
region 当前区域的数据对象
offset 数据对象开始的逻辑偏移量
buffer 当前区域的内存位置
size 当前区域的内存大小
bool 为YES时表示继续遍历
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data2 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t data3 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t data4 = dispatch_data_create_concat(data2, data3);
bool on = dispatch_data_apply(data4, ^bool(dispatch_data_t _Nonnull region, size_t offset, const void * _Nonnull buffer, size_t size) {
NSMutableData *totalData = [NSMutableData dataWithBytes:buffer length:size];
NSString *string = [[NSString alloc] initWithData:(NSData *)totalData encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
return YES;
});
NSLog(@"%@", @(on));
输出:因为data4是data2和data3组合而成的 所以会遍历2次 遍历完后 on=YES
1234567890
1234567890
1
当return NO;时
输出:返回NO时会停止遍历,而on也会为NO
1234567890
0
- 在数据对象data表示的区域中查找指定位置location的连续内存区域,并返回表示该区域的调度数据对象的副本及其在对象data中的逻辑偏移量
dispatch_data_t dispatch_data_copy_region(dispatch_data_t data,
size_t location, size_t *offset_ptr);
location 数据对象中的逻辑位置
offset_ptr size_t变量的指针,该变量将用返回的区域对象到查询的数据对象的起始位置的逻辑偏移量。
例:
dispatch_queue_t queue = dispatch_queue_create("data_queue", DISPATCH_QUEUE_CONCURRENT);
char charBuffer[] = {"1234567890"};
dispatch_data_t data2 = dispatch_data_create(charBuffer, 10*sizeof(char), queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
size_t size;
dispatch_data_t data3 = dispatch_data_copy_region(data2, 1, &size);
NSString *string = [[NSString alloc] initWithData:(NSData *)data3 encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
输出:不知为何这样输出 用法不明
1234567890 0
网友评论