We continue our Translating between ActionScript and Objective-C series with another auxiliary function that turns lengthy Objective-C code into one-liner you can reuse in all of your iOS ANEs.
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 got your hands dirty by implementing a shortcut for sending events from iOS to AIR
What will this part cover?
Today you will add another auxiliary function to your library of one-liners that make life easier and iOS ANE development – quicker.
The function looks like this:
1 |
BOOL isFREResultOK( FREResult errorCode, NSString * errMessage ) |
And does various checks for you: did a C AIR API call succeed, if not, what error code it returned and why.
Why is FREResult interesting?
If you have a peek inside FlashRuntimeExtensions.h, you will notice that most AIR C API functions return FREResult, which looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
typedef enum { FRE_OK = 0, FRE_NO_SUCH_NAME = 1, FRE_INVALID_OBJECT = 2, FRE_TYPE_MISMATCH = 3, FRE_ACTIONSCRIPT_ERROR = 4, FRE_INVALID_ARGUMENT = 5, FRE_READ_ONLY = 6, FRE_WRONG_THREAD = 7, FRE_ILLEGAL_STATE = 8, FRE_INSUFFICIENT_MEMORY = 9, FREResult_ENUMPADDING = 0xfffff /* will ensure that C and C++ treat this enum as the same size. */ } FREResult;  |
The implementation
Having a function that checks that result for you and does something when there is an error, i.e. anything different from FRE_OK, will save you having to repeat those checks over and over again. The isFREResultOK function defined below checks whether the FREResult we’ve got is an error and if it is, it sends an error event back to ActionScript and reports the error in Xcode’s debug console:
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 |
#pragma report errors /** * Checks if we have received an error code. * If yes, sends an error event to ActionScript * and logs it in the Xcode debugger's console. */ BOOL isFREResultOK( FREResult errorCode, NSString * errMessage ) { if ( FRE_OK == errorCode ) { // No error code, things went well. Nothing to report. return TRUE; } // Construct a string that will have the error code in it: NSString * messageToReport = [ NSString stringWithFormat: @"%@ %d", errMessage, errorCode ]; // Now log the message in the debug console: NSLog( @"%@", messageToReport ); // And send it as an event to ActionScript: sendMessage( MSG_ERROR, messageToReport ); return FALSE; } |
Result
Now you can do quick checks in any part of your code:
1 2 3 4 5 6 7 8 9 |
// Call one of the AIR C APIs that returns a FREResult: FREResult status = FREAcquireByteArray( byteArrayObjectAS, &byteArrayAS ); // Do a quick check on the FREResult: if ( !isFREResultOK( status, @"Could not acquire ByteArray." ) ) { // Optional: Send an error event to ActionScript return NULL; } |
Additional information
- A reminder about what all of those pointers and non-ActionScript concepts mean: how ActionScript data is represented in C
- See how to implement a one-liner for sending events from iOS to AIR
What’s next?
In the next tutorial you will implement a handler for exceptions from FRE* functions and see why you might want to do that.
Leave a Reply