Hello community! I have some answer about transferring arguments from lua to objective C
I have table which enters the function
t = {
a = 1,
b = “two”
}
I can easily parse this to nsdict
NSDictionary \* PluginWebView::Decode (lua\_State \* L) { //check type. if it's not a table it will throw an error/exception. luaL\_checktype(L, 1, LUA\_TTABLE); //print all of the key/value pairs in the Lua table NSMutableDictionary \* dictionary = [NSMutableDictionary dictionary]; for (lua\_pushnil(L); lua\_next(L, 1); lua\_pop(L, 1)) { const char \*keyName = NULL; int luaType = lua\_type(L, -2); switch (luaType) { case LUA\_TSTRING: // Fetch the table entry's string key. keyName = lua\_tostring(L, -2); break; case LUA\_TNUMBER: // The key will be a number if the given Lua table is really an array. // In this case, the key is an array index. Do not call lua\_tostring() on the // numeric key or else Lua will convert the key to a string from within the Lua table. keyName = [[NSString stringWithFormat:@"%td", lua\_tointeger(L, -2)] cStringUsingEncoding:NSASCIIStringEncoding]; break; } if (keyName == NULL) { //a valid key was not found. skip this table entry. continue; } //fetch the table entry's value in string form. //an index of -1 accesses the entry's value that was pushed into the lua stack by lua\_next() above. NSLog(@"%s", keyName); const char \*valueString = NULL; luaType = lua\_type(L, -1); switch (luaType) { case LUA\_TSTRING: valueString = lua\_tostring(L, -1); break; case LUA\_TBOOLEAN: valueString = [[NSString stringWithFormat:@"%d", lua\_toboolean(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding]; break; case LUA\_TNUMBER: valueString = [[NSString stringWithFormat:@"%f", lua\_tonumber(L, -1)] cStringUsingEncoding:NSASCIIStringEncoding]; break; } if (valueString == NULL) { valueString = ""; } [dictionary setValue:[NSString stringWithUTF8String:valueString] forKey:[NSString stringWithUTF8String:keyName]]; } NSLog(@"%@", dictionary); return [[dictionary copy] autorelease]; }
But if table has some nested tables kinda
t = {
a = 1,
b = “two”,
c = {
d = 4,
e = “five”
}
}
I want this table to be parsed recursive, so added
case LUA\_TTABLE: NSDictionary \* internalTable = PluginWebView::Decode(L); [dictionary setValue:internalTable forKey:[NSString stringWithUTF8String:keyName]]; break;
to lua_type case
The problem is it doesnt work and just loops itself, maybe some1 can tell me how I can parse these nested tables??
Thank u in advance