Looking for iOS version of printTableValuesXY ?

Hi everybody,

I’m looking for the iOS version of printTableValuesXY method that provided a good example for Android and can be found on the sample project

/Applications/CoronaEnterprise/Samples/SimpleLuaExtension

But when i look for the iOS sample code, there is just a empty code block 

printTableValuesXY( lua\_State \*L ) { // TODO NSLog( @"printTableValuesXY() not implemented" ); // Return 0 since this Lua function does not return any values. return 0; }

Since i have not got any experience with Obj-C language therefore i have no idea to implement this. Thus would you please help me to provide these code?

Thanks so much and looking forward for your response!

Here you go.  I’ve also filed a bug to get this code fleshed out generally.  Note that the Lua API is really just C and not Objective-C so it’s simpler than the sample might make it appear and any Lua C API tutorial should help you.

static int printTableValuesXY( lua\_State \*L ) { int luaTableStackIndex = 1; if ( lua\_istable( L, luaTableStackIndex ) ) { // Print only the entry's "x" and "y" in the Lua table. // See the private method below on how to access an entry within a Lua table. NSLog( @"printTableValuesXY()"); NSLog( @"{"); NSLog( @" [x] = %@", getLuaTableEntryValueFrom(L, luaTableStackIndex, "x")); NSLog( @" [y] = %@", getLuaTableEntryValueFrom(L, luaTableStackIndex, "y")); NSLog( @"}"); } else { NSLog(@"printTableValuesXY: argument is not a table"); } // Return 0 since this Lua function does not return any values. return 0; } static NSString \* getLuaTableEntryValueFrom( lua\_State \*L, int stackIndex, const char \*name ) { NSString \*valueString = nil; lua\_getfield( L, -1, name ); if (lua\_isnumber( L, -1)) { valueString = [NSString stringWithFormat:@"%f", lua\_tonumber( L, -1 )]; } else { valueString = [NSString stringWithFormat:@"%s", lua\_tostring( L, -1 )]; } lua\_pop( L, 1 ); return valueString; }

This sample code is written in the style of the existing code and isn’t intended to represent best practices.

Here you go.  I’ve also filed a bug to get this code fleshed out generally.  Note that the Lua API is really just C and not Objective-C so it’s simpler than the sample might make it appear and any Lua C API tutorial should help you.

static int printTableValuesXY( lua\_State \*L ) { int luaTableStackIndex = 1; if ( lua\_istable( L, luaTableStackIndex ) ) { // Print only the entry's "x" and "y" in the Lua table. // See the private method below on how to access an entry within a Lua table. NSLog( @"printTableValuesXY()"); NSLog( @"{"); NSLog( @" [x] = %@", getLuaTableEntryValueFrom(L, luaTableStackIndex, "x")); NSLog( @" [y] = %@", getLuaTableEntryValueFrom(L, luaTableStackIndex, "y")); NSLog( @"}"); } else { NSLog(@"printTableValuesXY: argument is not a table"); } // Return 0 since this Lua function does not return any values. return 0; } static NSString \* getLuaTableEntryValueFrom( lua\_State \*L, int stackIndex, const char \*name ) { NSString \*valueString = nil; lua\_getfield( L, -1, name ); if (lua\_isnumber( L, -1)) { valueString = [NSString stringWithFormat:@"%f", lua\_tonumber( L, -1 )]; } else { valueString = [NSString stringWithFormat:@"%s", lua\_tostring( L, -1 )]; } lua\_pop( L, 1 ); return valueString; }

This sample code is written in the style of the existing code and isn’t intended to represent best practices.