What data type should I use when converting a byte array from Swift to Objective-C

Markus Persson

I have a project where I write the production code in Swift and my unit tests in Objective-C (because I'm using OCMock I write my tests in Objective-C).

What I'm trying to do is to write a Buffer object where the writeBytes method takes a Byte array and a length, like this:

func writeBytes(data: [Byte], length: Int)

In my unit test target I want to test this method, but the problem is that it won't convert this method to Objective-C. By looking in my -Swift.h file I see the following:

@interface Buffer : Object
@property (nonatomic) NSInteger capacity;
@property (nonatomic) NSInteger size;
- (instancetype)initWithCapacity:(NSInteger)capacity OBJC_DESIGNATED_INITIALIZER;
- (void)reset;
- (NSInteger)size;
- (BOOL)isFilled;
- (BOOL)isEmpty;
@end

As you can see the writeBytes method is missing. I've tried different data types to see if it appears: UInt8, UnsafePointer<UInt8>, but none of them work.

For the sake of debugging I tried using a String array and that gets converted to Objective-C without any problems.

What data type should I use for my Byte array to make the writeBytes method available in Objective-C?

Martin R

The Swift type [Byte] is not representable in Objective-C, but you can work with a pointer to the bytes:

func writeBytes(data: ConstUnsafePointer<Byte>, length: Int) { ... }

var data : [Byte] = [1, 2, 3]
writeBytes(data, length: data.count)

This mapped to Objective-C as

- (void)writeBytes:(Byte const *)data length:(NSInteger)length;

so that you can call it like

Byte data[] = {1, 2, 3};
[buffer writeBytes:data length:3];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What data type I should use for versioning?

From Dev

What type of data should I use (MySQL)?

From Dev

Expected Declaration when converting from Objective C to Swift

From Dev

Expected Declaration when converting from Objective C to Swift

From Dev

Are there security concerns I should keep in mind when converting my app from Obj-C to Swift?

From Dev

Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project?

From Dev

Image data loss when converting to byte array

From Dev

Converting an Objective C array declaration to Swift

From Dev

Should I use ARC when coding in Objective-C++?

From Dev

Convert Objective-C to Swift - what to use for unsigned type?

From Dev

Converting From Objective-C to Swift

From Dev

Swift - Help Converting from Objective C Syntax

From Dev

Converting a code from Swift to Objective C?

From Dev

what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

From Dev

What data structure should I use to look up a string from an array of possible strings?

From Dev

What data type should I use for IETF language codes?

From Dev

MySQL - what data type should i use to store a set of strings

From Dev

MySQL - What data type should I use to store a Map field

From Dev

What data type should I use for large and accurate numbers?

From Dev

What data type should I use for OpenGL-es vertices?

From Dev

What data type should I use for large and accurate numbers?

From Dev

What data type should I use for variable length with German characters?

From Dev

It is possible to use array reduce concept from swift in objective c?

From Dev

Should I learn Swift or Objective C for iOS

From Dev

When should I use data type classes in JavaScript

From Dev

Linker error when passing data from Swift to Objective C

From Dev

What settings should I use to keep the colour correct when converting a PDF to x-3:2002 with ghostscript?

From Dev

What language I should use when printing something from a kiosk

From Dev

Swift converting Byte Array into String

Related Related

  1. 1

    What data type I should use for versioning?

  2. 2

    What type of data should I use (MySQL)?

  3. 3

    Expected Declaration when converting from Objective C to Swift

  4. 4

    Expected Declaration when converting from Objective C to Swift

  5. 5

    Are there security concerns I should keep in mind when converting my app from Obj-C to Swift?

  6. 6

    Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project?

  7. 7

    Image data loss when converting to byte array

  8. 8

    Converting an Objective C array declaration to Swift

  9. 9

    Should I use ARC when coding in Objective-C++?

  10. 10

    Convert Objective-C to Swift - what to use for unsigned type?

  11. 11

    Converting From Objective-C to Swift

  12. 12

    Swift - Help Converting from Objective C Syntax

  13. 13

    Converting a code from Swift to Objective C?

  14. 14

    what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

  15. 15

    What data structure should I use to look up a string from an array of possible strings?

  16. 16

    What data type should I use for IETF language codes?

  17. 17

    MySQL - what data type should i use to store a set of strings

  18. 18

    MySQL - What data type should I use to store a Map field

  19. 19

    What data type should I use for large and accurate numbers?

  20. 20

    What data type should I use for OpenGL-es vertices?

  21. 21

    What data type should I use for large and accurate numbers?

  22. 22

    What data type should I use for variable length with German characters?

  23. 23

    It is possible to use array reduce concept from swift in objective c?

  24. 24

    Should I learn Swift or Objective C for iOS

  25. 25

    When should I use data type classes in JavaScript

  26. 26

    Linker error when passing data from Swift to Objective C

  27. 27

    What settings should I use to keep the colour correct when converting a PDF to x-3:2002 with ghostscript?

  28. 28

    What language I should use when printing something from a kiosk

  29. 29

    Swift converting Byte Array into String

HotTag

Archive