Żabojad
Żabojad

Reputation: 3095

Objective-C how to convert a NSArray to Byte array?

I'm not very experienced with objective-c and I would need to advise for the following use case:

I'm coding a React native module for ios and one of the native methods on the ios side receives an NSArray containing integers (each integer has a value between 0 and 255).

In my objective-c code, I need to convert this NSArray to a Byte bytes[] to pass it then to some SDK I use.

How can I do that conversion?

Upvotes: 1

Views: 1118

Answers (1)

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

NSArray* array = @[@(1), @(5), @(115), @(34)];

Byte* bytes = calloc(array.count, sizeof(Byte));

[array enumerateObjectsUsingBlock:^(NSNumber* number, NSUInteger index, BOOL* stop){

    bytes[index] = number.integerValue;
}];

doWhateverWithBytes(bytes);
free(bytes);

assuming that doWhateverWithBytes accepts Byte bytes[]

Upvotes: 3

Related Questions