Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 luaoc.callStaticMethod 调用 Objective-C 方法时报错 not support return type #99

Closed
Maxize opened this issue Dec 25, 2017 · 1 comment

Comments

@Maxize
Copy link
Contributor

Maxize commented Dec 25, 2017

今天写了一个 lua 调用 iOS 的同步回调遇到了这个问题。
一开始直接跟的源码,然后发现使用 NSMethodSignature 获取到的 methodReturnType 就是 'B'。
之前没怎么写过 oc 代码,所以 Google 之。

发现了在 Quick-x 最早的时候( 2 Nov 2015 )有这么一个 issue:

使用luaoc.callStaticMethod调用Objective-C方法时报错not support return type

顺便去看了一下 cocos2d-x 的源码,发现也修复了,追溯到是在 2017 年 6 月份修复的

luaoc static function return type BOOL #17956

解决方案: 使用 @encode 关键字转化来进行字符串比较。

以下是改动后的代码:

// in CCLuaObjcBridge.mm on line 123
if (returnLength > 0)
{
    if (strcmp(returnType, @encode(id)) == 0)
    {
        id ret;
        [invocation getReturnValue:&ret];
        pushValue(L, ret);
    }
    else if (strcmp(returnType, @encode(BOOL)) == 0) // BOOL
    {
        char ret;
        [invocation getReturnValue:&ret];
        lua_pushboolean(L, ret);
    }
    else if (strcmp(returnType, @encode(int)) == 0) // int
    {
        int ret;
        [invocation getReturnValue:&ret];
        lua_pushinteger(L, ret);
    }
    else if (strcmp(returnType, @encode(float)) == 0) // float
    {
        float ret;
        [invocation getReturnValue:&ret];
        lua_pushnumber(L, ret);
    }
    else
    {
        NSLog(@"not support return type = %s", returnType);
        lua_pushnil(L);
    }
}

@u0u0 麻烦看一下,谢谢。

@u0u0
Copy link
Owner

u0u0 commented Jan 4, 2018

thanks,accept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants