尝试调用类方法时出错:尝试索引本地"self"(nil值)-Lua [英] Error while trying to call a class method: attempt to index local 'self' (a nil value) - Lua

查看:562
本文介绍了尝试调用类方法时出错:尝试索引本地"self"(nil值)-Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以在TI-Nspire计算器上运行的lua脚本.问题是在运行脚本时,调用button:activate()方法时出现错误Attempt to index local 'self' (a nil value).解析器说错误在下面的代码的第8行中.有问题的代码如下:

I'm creating a lua script that should run on the TI-Nspire calculator. The problem is that while running my script I get the error Attempt to index local 'self' (a nil value) when the button:activate() method is called. The parser says the error is in the 8th line in the code below. The problematic code is as follows:

button = class(view)

function button:init()
    self.selected = false
end

function button:activate()
    self.selected = true
end

我这样调用激活函数:

item = button()
local action = "activate"
local arguments = {}
item[action](unpack(arguments))

我知道class()函数在常规" Lua中不存在,它是TI-Nspire Lua实现中可用的函数.您可以在此处找到它的定义和用法.

I am aware the class() function doesn't exist in "stock" Lua, it's a function available in the TI-Nspire Lua implementation. You can find its definition and usage here.

推荐答案

obj:methodname(args)obj.methodname(obj,args)的糖.因此,如果要使用语法item[action](unpack(arguments)),则需要使用item[action](item,unpack(arguments)).否则,如果可以显式使用方法,请尝试item:activate(unpack(arguments)).

obj:methodname(args) is sugar for obj.methodname(obj,args). So, if you want to use the syntax item[action](unpack(arguments)), you need to use item[action](item,unpack(arguments)). Otherwise, try item:activate(unpack(arguments)) if you can use method explicitly.

这篇关于尝试调用类方法时出错:尝试索引本地"self"(nil值)-Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆