Lua类型判断和转换的简易方法
2015-11-03 11:28
344 查看
-- 如果待判断的是一个变量 local theType = type(theObject) if theType == "number" then -- 是数字 elseif theType == "string" then -- 是字符串 elseif theType == "table" then -- 是Lua表 end -- 如果带判断是一个字符串,要判断是否可以转成数字, 则 local theNumber = tonumber(theObject); if theNumber ~= nil then -- theNumber 就是得到的数字 else -- 转换数字失败 这时theNumber = nil end -- 如果支持toLua的工具,也可以使用 local theNumber = checkint(theObject) -- ↑使对象转换为int -- 顺带一提,强制类型转换,以cocos2dx为例,转Node为Sprite local theObjectNode = xxLayer:getChildByTag(233) if theObjectNode ~= nil then local theSprite = tolua.cast(theObjectNode , "cc.Sprite") end
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨