Reputation: 259
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;
Why need do this ?
I can't understand the *(id*)
also the id* ret = afunc()
,when delete this star , it also works well.
Upvotes: 3
Views: 175
Reputation: 2498
afunc is referencing the function (void *)wax_copyToObjc(...). The intent of this function is to translate a Lua object into a C or Objective-C value. Because it could be a primitive type or an objective-c instance it doesn't know what it is going to return. So always returns a pointer to void (meaning a pointer to something unknown). In the case of an id, it will return a pointer to an id.
It might be easier to explain what is happening with an int, it will alloc space for the int and copy the its value:
value = calloc(sizeof(int), 1)
*(int *)value = lua_tointeger(L, stackIndex)
(int *)value is translates to "value is a pointer to an int"
adding the * in front of it like *(int *)value
translates to "copy the int to the alloc'd memory that value points to."
In your example:
id *ret = afunc(); // returns a pointer to an id
id lastValue = *(id*)ret; // dereferences the pointer to id so it is just an id
Upvotes: 1
Reputation: 12235
This looks weird. Since the issues came from what looks like C Code, I suggest to not use id
. Although it's supposed to be the same as void*
, devs tend to use the first in ObjC code, and the latter in C code. You'd gain in clarity.
Now, I don't see why void* value = instance;
and void* ret = afunc();
should not do as expected.
Upvotes: 0