- openrest最佳实践推荐的方法:
-----------------------------------------
-- @description 对table判空
-- @param t 要验证的table
-- @return true table是空 falsetable 不是空
-----------------------------------------
function isTableEmpty (t)
if t == nil or next (t) == nil then
return true
else
return false
end
end
- 开发中用到的:
-----------------------------------------
-- @description 对table判空
-- @param t 要验证的table
-- @return true table是空 falsetable 不是空
-----------------------------------------
function isTableEmpty (t)
if t and #t > 0 then
return false
else
return true
end
end
本文介绍了两种在Lua中判断Table是否为空的有效方法。第一种方法使用next函数来遍历table,如果table为空或者没有元素,则返回true;第二种方法检查table的长度属性,如果长度大于0则认为table非空。这两种方法可以帮助开发者在实际开发过程中更好地处理Lua中的table数据结构。
2万+

被折叠的 条评论
为什么被折叠?



