【问题标题】:Corona sdk "OR" operatorCorona sdk“或”运算符
【发布时间】:2014-09-10 22:28:42
【问题描述】:

我正在开发一个从网络请求返回 json 表的应用程序。当用户输入 3 个字母的机场代码并在 newText 对象中返回。如果机场 LAX 中的类型,json 文件中的 stateCode 作为 CA 返回,但如果机场代码中的类型来自不同国家/地区,则返回错误,因为返回的内容没有 stateCode。

所以我尝试在下面的代码中使用“或”运算符。

display.newText(myData.airports[1].stateCode or myData.airports[1].countryCode)

如何返回 stateCode,如果没有 stateCode,返回 countryCode?​​p>

【问题讨论】:

    标签: json sdk lua coronasdk operator-keyword


    【解决方案1】:

    你应该试试这个

    local textValue = myData.airports[1].stateCode or myData.airports[1].countryCode
    

    然后您可以将其用作 newText 的值。如果 stateCode 是 nilfalse,它将使用 countryCode。

    【讨论】:

    • 哦!好的,我试试这个。
    • 您可能需要添加第三个字符串,以防两个变量都为零(可能是您的数据丢失的机场):local textValue = myData.airports[1].stateCode 或 myData.airports [1].countryCode 或“未知机场”
    【解决方案2】:

    如果您在任何函数中使用此代码,

    display.newText(myData.airports[1].stateCode or myData.airports[1].countryCode)
    

    那你直接这样返回,

    return myData.airports[1].stateCode or myData.airports[1].countryCode
    

    这将自动返回可用的值。

    【讨论】:

    • 我没有在函数中使用它。我正在创建各种文本显示对象
    • 如何在函数中使用 i?
    • 您最好将您要修改的部分代码分享给我。
    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2013-12-18
    • 2017-03-14
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多