alibaba / wax

Wax is a framework that lets you write native iPhone apps in Lua.
http://github.com/probablycorey/wax
MIT License
1.84k stars 280 forks source link

请问要获取OC方法中抛出的Error用Lua该怎么写(NSError **)? #50

Closed rtyu128 closed 5 years ago

rtyu128 commented 7 years ago

比如用系统方法解析JSON数据:

NSError *error = nil;
NSDictionary *parseDict = [NSJSONSerialization JSONObjectWithData:data
                                                          options:NSJSONReadingAllowFragments
                                                            error:&error];

第三个参数是NSError **类型的,改用Lua该如何表示?

local err = ???
local parseDict = NSJSONSerialization:JSONObjectWithData_options_error(data, 8, err)
if err then --?
...
end

试了下err = {},会提示类型不匹配Can't convert table to wax_instance_userdata.,也不能用nil呀,求教,非常感谢~

y138828 commented 7 years ago

可以用parseDict来代替 local parseDict = NSJSONSerialization:JSONObjectWithData_options_error(data, 8, nil) if parseDict then -- code here end

源码当中只有 LUA_TNIL,LUA_TNONE, LUA_TUSERDATA, LUA_TLIGHTUSERDATA支持转换指针 指向指针的指针不被支持,可以手动修改源码支持

rtyu128 commented 7 years ago

嗯,最后用的就是判断parseDict是否为空绕了一下,Thx~