typedef struct _wax_instance_userdata {
    id instance;
    BOOL isClass;
    Class isSuper;
    BOOL actAsSuper;
} wax_instance_userdata;

https://github.com/probablycorey/wax/blob/master/lib/wax_helpers.m#L497
void* afunc(){ // the function is too long
    void *value = nil;
    // ...
    wax_instance_userdata *instanceUserdata = (wax_instance_userdata *)luaL_checkudata(L, stackIndex, WAX_INSTANCE_METATABLE_NAME);

    instance = instanceUserdata->instance;

    *(id *)value = instance;
    return value;
}

https://github.com/probablycorey/wax/blob/master/lib/wax.m#L243
id* ret = afunc(); //same without this * .
id lastValue = *(id*)ret;
//now I can use lastValue;

为什么需要这样做?
我听不懂*(id*)还有id* ret = afunc(),当删除此星标时,效果也很好。

最佳答案

afunc引用了函数(void *)wax_copyToObjc(...)。此功能的目的是将Lua对象转换为C或Objective-C值。因为它可能是原始类型或 objective-c 实例,所以它不知道它将返回什么。因此,始终返回指向void的指针(意味着指向未知对象的指针)。如果是id,它将返回一个指向id的指针。

解释一个int发生了什么可能会更容易,它将为int分配空间并复制其值:

value = calloc(sizeof(int), 1)
*(int *)value = lua_tointeger(L, stackIndex)

(int *)value转换为“value是一个指向int的指针”
*(int *)value一样在其前面添加*表示“将int复制到值所指向的已分配内存中”。

在您的示例中:
id *ret = afunc(); // returns a pointer to an id
id lastValue = *(id*)ret; // dereferences the pointer to id so it is just an id

10-07 19:43
查看更多