How to transfer complex data structure from iOs native extension to HaXe?


I have, let’s say, NSArray containing NSStrings in my objective c code of native extension. I need to have it in HaXe code as Array<String>. What is the right way to transfer it?

As far as i understand, i need first to transfer it to some function in cpp code (declared in ExternalInterface.cpp) which then calls (via val_call) HaXe callback and passes data to it. I’m pretty sure there is no way to transfer NSArray or even just NSString as is. But i cannot figure out how to convert it to something that can be passed through.

I’ve found from other extensions source code that NSString should be passed as const char* via [myNSStringVar UTF8String], but still have no idea on how to pass array.

Thanks in advance!

This is the sort of thing JSON is designed for.

  1. Convert your NSArray to JSON.
  2. Pass it to Haxe.
  3. Parse it.
  4. Done!
1 Like

Here’s a little example of allocating a Haxe array on the C++ side:

You should be able to use alloc_string or alloc_wstring if you convert the NSString to const char*, std::string or a wstring

1 Like

Great advice, thanks!

This definitely should do the trick on transferring small structures. But in cases of some “big data” JSON could fail. I’ve tried to pack ~140kb string to JSON recently on neko target - failed as expected.

Anyway, packing to JSON reflects “keep it simple stupid” approach. And sometimes it is all we really need. :wink:

It is exactly what i was seeking!

And i’ve just took a look at hx/CFFI.cpp in hxcpp project - good place to learn how allocs and vals are working. It isn’t documented well at the moment, am i right? At least i cannot find available documentation for this.

I’ll describe and share my solution on topic after implementation.

Thanks, @singmajesty!

Yeah, I look at sources :wink:

Also :slight_smile:

Aw! Thank you for the right link, things are much clearer now. Very useful indeed.

I didn’t found this before as i was googling for “haxe alloc_array” and there’s only one mention of “haxe” on the page: “Haxe Foundation” in footer. :smiley: