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:
- sendMessage – a shortcut for sending events from iOS to AIR;
- isFREResultOK, which checks the FREResult returned by AIR C API functions;
- hasThrownException, which does another check: did your last call to an AIR C API throw an exception and if yes, what did that mean?
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
1 |
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.
The implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#pragma properties /** * 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 ); } |
Result
This is how you use setFREObjectProperty in your Objective-C code:
1 2 3 |
// Set the length of an ActionScript ByteArray: FREObject lengthAS = getFREObjectFromUInt32( bufferLength ); setFREObjectProperty( *byteArrayObjectAS, ( const uint8_t* ) "length", lengthAS ); |
What’s next?
- In the next article you will see how to implement a mirror auxiliary function, which will read properties of ActionScript objects for you.
Leave a Reply