Requirements
Prerequisite knowledge
To make the most of this article, you’ll need a good understanding of ActionScript, C, Flash Builder, Xcode, and using tools from the command line.
Additional required other products
User level
Intermediate

In Part 1 of this series I showed how to exchange basic types such as Number, int, uint, String, and Boolean data between ActionScript 3 and C (or C++, C#, or Objective-C) for native extensions on iOS.  In Part 2, I showed how to use the same approach with arrays and Vector objects.  In this article, I extend the technique to custom objects.
 
If you haven’t done so already, I recommend reading Part 1 and Part 2 before proceeding with this article.
 
 

 
Exchanging custom objects between ActionScript 3 and C

There are situations in which you want to transfer a custom object instead of a predefined object or a basic type. For example, you may want to exchange an array of positions in three dimensions. Position is, in this case, defined by an object with x, y, and z properties.
 
The first step is to initiate an Array instance. You can use the same approach outlined in Part 2: Exchanging Vector and Array objects between ActionScript 3 and C, C++, or Objective-C:
 
 
uint32_t arr_len = 10; // count of positions FREObject objectsPosition = NULL; FRENewObject((const uint8_t*)"Array", 0, NULL, &objectsPosition, NULL); FRESetArrayLength(objectsPosition, arr_len);
Next, define a custom Object, as you did with the Array in Part 2.  Here is an example from the getArrayOfPositions() function in the sample code:
 
// loop through array length and fill it with data for(uint32_t i=0;i<arr_len;i++){ FREObject position; // create an instance of Object and save it to FREObject position FRENewObject((const uint8*)"Object", 0, NULL, &position,NULL); // populate temporary vars x, y, z FREObject xPos; FREObject yPos; FREObject zPos; FRENewObjectFromInt32(10, &xPos); FRENewObjectFromInt32(20, &yPos); FRENewObjectFromDouble(30, &zPos); // fill properties of FREObject position FRESetObjectProperty(position, (const uint8*)"x", zPos, NULL); FRESetObjectProperty(position, (const uint8*)"y", yPos, NULL); FRESetObjectProperty(position, (const uint8*)"z", zPos, NULL); // add position to the array FRESetArrayElementAt(objectsPosition, i, position); }
The sample code for this article contains the following ActionScript functions:
 
public function getArrayOfPositions():Array{ return context.call("getArrayOfPositions"); }
Here is the associated C code:
 
FREObject getArrayOfPositions(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ uint32_t arr_len = 10; // count of positions FREObject objectsPosition = NULL; FRENewObject((const uint8_t*)"Array", 0, NULL, &objectsPosition, NULL); FRESetArrayLength(objectsPosition, arr_len); // loop through array length and fill it with data for(uint32_t i=0;i<arr_len;i++){ FREObject position; // create an instance of Object and save it to FREObject position FRENewObject((const uint8_t*)"Object", 0, NULL, &position,NULL); // populate temporary vars x, y, z FREObject xPos; FREObject yPos; FREObject zPos; FRENewObjectFromInt32(10, &xPos); FRENewObjectFromInt32(20, &yPos); FRENewObjectFromDouble(30, &zPos); // fill properties of FREObject position FRESetObjectProperty(position, (const uint8_t*)"x", zPos, NULL); FRESetObjectProperty(position, (const uint8_t*)"y", yPos, NULL); FRESetObjectProperty(position, (const uint8_t*)"z", zPos, NULL); // add position to the array FRESetArrayElementAt(objectsPosition, i, position); } return objectsPosition; }
The code above shows how to transfer instances of Object type.  You can also define your own type; for instance a Position type that includes x, y, and rotation properties.
 
The sample code also includes support for getting an individual instance of this Position type. The class is defined in Position.as:
 
 
package com.krcha { [RemoteClass(alias="com.krcha.Position")] public class Position { public var x:uint; public var y:uint; public var rotation:Number; } }
The ActionScript function is in IOSExtension.as:
 
public function getPosition():Position{ return context.call("getPosition") as Position; }
Here is the related C function from IOSExtension.m:
 
FREObject getPosition(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ uint32_t arr_len = 10; // count of positions FREObject position = NULL; FRENewObject((const uint8_t*)"com.krcha.Position", 0, NULL, &position, NULL); FRESetArrayLength(position, arr_len); FREObject xPos; FREObject yPos; FREObject rotation; FRENewObjectFromInt32(10, &xPos); FRENewObjectFromInt32(20, &yPos); FRENewObjectFromDouble(1.57f, &rotation); FRESetObjectProperty(position, (const uint8_t*)"x", xPos, NULL); FRESetObjectProperty(position, (const uint8_t*)"y", yPos, NULL); FRESetObjectProperty(position, (const uint8_t*)"rotation", rotation, NULL); return position; }

 
Where to go from here

To learn more about native extensions check out the Native C API Reference for Adobe AIR extensions.