Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua-Cocoa-字符串Concat-简单表到NSArray_Cocoa_Lua - Fatal编程技术网

Lua-Cocoa-字符串Concat-简单表到NSArray

Lua-Cocoa-字符串Concat-简单表到NSArray,cocoa,lua,Cocoa,Lua,Mac OS X 10.5兼容性,需要Lua 5.0兼容性(因此无法使用当前批次的LuaObjc网桥。) 我的lua脚本生成一个包含上万个字符串的索引表 基本问题:如何使用换行符将这些字符串快速转换为一个字符串 美中不足:即使使用垃圾收集友好的concat代码(在stackoverflow中提供),结果也会花费很长时间。(暴力解决方案为10秒vs 1分钟。) 建议的解决方案:将工作转移给Cocoa,使用NSArray的-componentsJoinedBything方法,可以在几分之一秒内完成

Mac OS X 10.5兼容性,需要Lua 5.0兼容性(因此无法使用当前批次的LuaObjc网桥。)

我的lua脚本生成一个包含上万个字符串的索引表

基本问题:如何使用换行符将这些字符串快速转换为一个字符串

美中不足:即使使用垃圾收集友好的concat代码(在stackoverflow中提供),结果也会花费很长时间。(暴力解决方案为10秒vs 1分钟。)

建议的解决方案:将工作转移给Cocoa,使用NSArray的-componentsJoinedBything方法,可以在几分之一秒内完成

新的粉身碎骨:如何从Lua到Cocoa获取表格数据

该脚本调用已注册的C函数,并将其传递给表。C函数尝试获取堆栈上的表:

// Get an NSArray of strings from the first argument on the stack (a table).
NSArray *strings = nsArrayFromIndexedTable(luaState, index_1Based);

...

// Given a simple table consisting of numbers or strings, returns an NSArray.
// Nested subtables are not followed.

NSArray * nsArrayFromIndexedTable(lua_State *L, int argIdx)
{
    // (Allegedly) stops GC.
    lua_setgcthreshold(L, INT_MAX);

    // Arg must be a table.
    luaL_checktype(L, argIdx, LUA_TTABLE);

    // Get num elements in table, build an array with that many.
    int count = luaL_getn(L, 1);

    NSMutableArray *array = [NSMutableArray arrayWithCapacity: count];

    int i;
    for (i = 1; i <= count; i++) {

        lua_rawgeti(L, argIdx, i);
        int valueType = lua_type(L, -1);
        id value = 0x00;

        if (valueType is_eq LUA_TNUMBER) {
            value = [NSNumber numberWithDouble:lua_tonumber(L, -1)];
        } else if (valueType is_eq LUA_TSTRING) {
            value = [NSString stringWithUTF8String:lua_tostring(L,  -1)];
        }

        if (value) {
            [array addObject:value];
        }
    }

    // Resume GC
    lua_setgcthreshold(L, 0);    // INTERMITTENT EXC_BAD_ACCESS CRASH HERE!!!!

    return array;
}
//从堆栈(表)上的第一个参数获取字符串的NSArray。
NSArray*字符串=nsArrayFromIndexedTable(luaState,基于索引);
...
//给定一个由数字或字符串组成的简单表,返回一个NSArray。
//不遵循嵌套子表。
NSArray*nsArrayFromIndexedTable(lua_State*L,int argIdx)
{
//(据称)停止GC。
lua_设置阈值(L,INT_最大值);
//Arg必须是表。
luaL_checktype(L,argIdx,LUA_TTABLE);
//获取表中的num个元素,并用这些元素构建一个数组。
int count=luaL_getn(L,1);
NSMUTABLEARRY*array=[NSMUTABLEARRY阵列容量:计数];
int i;
对于(i=1;i

应该是:

int count = luaL_getn(L, argIdx);    

因此,您可能得到了不正确的行数并扫描了表的末尾。

可能您的C堆栈增长过多。我不熟悉Cocoa,但我猜Lua值不一定总是可以访问的-应该将字符串复制到NSString中。如果是这样,请尝试包含一个
Lua\u pop(L,1)
在循环结束时,清理C堆栈并防止其增长。

谢谢Graham。我已经做了更改,但遗憾的是,问题仍然存在。它正在luaC_collectgarbage()中消亡。干杯。
int count = luaL_getn(L, argIdx);