How to convert dispatch_data_t to NSData?

后端 未结 2 1701
北荒
北荒 2021-01-12 07:32

Is this the right way?

// convert
const void *buffer = NULL;
size_t size = 0;
dispatch_data_t new_data_file = dispatch_data_create_map(data, &buffer, &am         


        
相关标签:
2条回答
  • 2021-01-12 07:37

    For the most part, your code is correct. +initWithBytes:length: will copy the buffer sent in so, you don't have to worry about freeing the buffer after the data, you can safely free the data first.

    According to the documentation, you do NOT free the data after you are done with it:

    If you specify non-NULL values for buffer_ptr or size_ptr, the values returned in those variables are valid only until you release the newly created dispatch data object. You can use these values as a quick way to access the data of the new data object.

    You simply release the new_data_file variable (ARC will not do this for you).

    0 讨论(0)
  • 2021-01-12 07:58

    Since iOS 7 and macOS 10.9 (Foundation Release Notes) dispatch_data_t is an NSObject (NSObject <OS_dispatch_data>) in 64 bit apps.

    dispatch_data_t can now be freely cast to NSData *, though not vice versa.

    0 讨论(0)
提交回复
热议问题