Corona模拟器中的布尔型参数问题

我在使用Corona SDK开发移动应用时,试图使用 system.setPreferences()/ system.getPreference() 函数保存设置。在我的安卓手机上可以正常工作,但在Corona模拟器中工作方式如下:

  1. 首次运行以下代码:
    system.setPreferences("app", {
        test = true
    })
    print(system.getPreference("app", "test", "boolean"))

将输出 true,与预期相符

  1. 第二次,我尝试获取未保存的参数(应该已经在上一步中保存了)
print(system.getPreference("app", "test", "boolean"))

输出如下:

ERROR: C:\Users\kezzyhko\Desktop\MobileGame\main.lua:7: system.getPreference() - Preference's string value cannot be converted to boolean. nil

  1. 尝试将其作为字符串获取
print(system.getPreference("app", "test", "string"))

将输出 1

我试图查看存储参数的 C:\Users\kezzyhko\AppData\Local\Corona Labs\Corona Simulator\Sandbox\mobilegame-370DE4D889B5BBC98141FE51641482AD\.system\CoronaPreferences.sqlite 文件,并确实发现布尔值保存为 0/ 1。如果我手动将其编辑为 true,则代码按预期工作:

print(system.getPreference("app", "test", "boolean"))

将输出 true

这是已知问题吗(我找不到任何信息)?有没有办法在不使用污码的情况下解决它?

点赞
用户88888888
用户88888888

这听起来像是技术上的问题。当然,它们是“boolean”,因为它们是1和0,但是由于它们存储在文件中,它们很可能以文本形式存储,即字符串。

因此,当您加载数据时,您可能需要始终将它们作为字符串接收,然后手动说明0为false,1为true。

我建议您使用JSON / SQL保存数据。 这是有关JSON的简单教程:https://docs.coronalabs.com/tutorial/data/jsonSaveLoad/index.html

2020-04-21 06:01:40