In this instalment of our Translating between ActionScript and Objective-C we add the mirror function of the one you implemented for writing properties of ActionScript object: now you will see how to read ActionScript object properties 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?
- setFREObjectProperty to help you change the properties of ActionScript objects on the native side of your ANE.
What will this part cover?
Being able to read an ActionScript object’s property in your C code is just as useful as being able to set it. Both the object we read from and the property we read are wrapped in FREObject. The helper functions you will define in the next articles will help you convert the value of the FREObject property to a native type: int, NSString, etc.
The AIR C API call to note here is FREGetObjectProperty.
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 28 29 30 31 32 |
/** * Gets a named property from an ActionScript object. * Calling FREObject phase = getFREObjectProperty( asEvent, ( const uint8_t * ) "eventPhase" ); * where asEvent is a FREObject instance, * is like calling var phase : int = event.eventPhase; in ActionScript. * * @param objAS The ActionScript object. * * @param propertyName The name of the integer property to be read. * * * @return The value of the property, converted to NSInteger or -1 if the call was unsuccessful. * */ FREObject getFREObjectProperty( FREObject objAS, const uint8_t * propertyName ) { assert( NULL != propertyName ); assert( NULL != objAS ); FREObject valueAS = NULL; FREObject thrownException = NULL; FREResult status = FREGetObjectProperty( objAS, propertyName, &valueAS, &thrownException ); // Check if we encountered an error: if ( !isFREResultOK( status, @"Could not read an ActionScript object's property" ) || hasThrownException( thrownException ) ) { return NULL; } return valueAS; } |
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 |
/** * Reads an integer value from an ActionScript object property. * Calling NSInteger phase = getNSIntegerFromFREObjectProperty( asEvent, ( const uint8_t * ) "eventPhase" ); * where asEvent is a FREObject instance, * is like calling var phase : int = event.eventPhase; in ActionScript. * * @param objAS The ActionScript object. * * @param propertyName The name of the integer property to be read. * * * @return The value of the property, converted to NSInteger or -1 if the call was unsuccessful. * */ NSInteger getNSIntegerFromFREObjectProperty( FREObject objAS, const uint8_t * propertyName ) { FREObject valueAS = getFREObjectProperty( objAS, propertyName ); if ( NULL == valueAS ) { return -1; } NSInteger value = getNSIntegerFromFREObject( valueAS ); return value; } |
Result
Now you can read the hours property of a Date ActionScript object (represented by dateAS below) in your native code like this:
1 |
NSInteger hour = getNSIntegerFromFREObjectProperty( dateAS, ( const uint8_t * ) "hours" ); |
What’s next?
TODO
Leave a Reply