We continue with our Translating between ActionScript and Objective-C series by adding a couple of mirror functions for reading and writing properties of ActionScript objects in your native Objective-C code.
What was covered so far?
- We started with an introduction of what to expect from the series and how tutorials will be structured
- Then we laid some ground work and saw how ActionScript data is represented in C
- You have so far added the following auxiliary functions to your helper function library:
What will this part cover?
The AIR C API strives to make most of what you can do in ActionScript possible to replicate in C. For example, if you have an object of type Date, named date and wanted to set its day property, you can do that. Only instead of the ActionScript one-liner
date.day = 1;
you need to write a few more lines and to a few checks to achieve the same in C. This is what the next function helps you with: set an arbitrary property of an arbitrary object, so long as you have a pointer to the object and know the property name.
Note that both the object whose property you are changing and the value you are changing to need to be FREObjects.
The call that does the actual job here is AIR C API’s FRESetObjectProperty.
* Sets a named property of an ActionScript object.
* Calling setFREObjectProperty( asEvent, ( const uint8_t * ) "type", eventTypeAS );
* where asEvent and eventTypeAS are FREObject instances,
* is like calling event.type = "SOMETHING"; in ActionScript.
* @param objAS The ActionScript object.
* @param propertyName The name of the property to be set.
* @param propertyValue The value the property should be set to.
void setFREObjectProperty( FREObject objAS, const uint8_t * propertyName, FREObject propertyValue )
assert( NULL != objAS );
assert( NULL != propertyName );
assert( NULL != propertyValue );
FREObject thrownException = NULL;
FREResult status = FRESetObjectProperty( objAS, propertyName, propertyValue, &thrownException );
// Check if we encountered an error:
isFREResultOK( status, @"Could not set an ActionScript object's property" );
hasThrownException( thrownException );
This is how you use setFREObjectProperty in your Objective-C code:
// Set the length of an ActionScript ByteArray:
FREObject lengthAS = getFREObjectFromUInt32( bufferLength );
setFREObjectProperty( *byteArrayObjectAS, ( const uint8_t* ) "length", lengthAS );
- In the next article you will see how to implement a mirror auxiliary function, which will read properties of ActionScript objects for you.