gpt4 book ai didi

ios - DDMathParser:解析包含 $ 的动态公式以引用字典中的对象值

转载 作者:行者123 更新时间:2023-11-28 15:25:25 24 4
gpt4 key购买 nike

我正在努力使用 DDMathParser 框架来满足我的表达要求。我有字段的 JSON & 基于表达式,某些字段可以设置为必填、隐藏或设置它的值。

示例 JSON 中 required 标记中的表达式不固定,因此不知道如何实现表达式的动态方法。

[
{
"name": "firstName",
"value": "Ameer",
**"required": true**
},
{
"name": "lastName",
"value": "Shaikh",
**"required": "$firstName != ‘’"**
},
{
"name": "designation",
"value": "",
**"required": "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"**
},
{
"name": "location",
"value": "",
**"hidden": false**
}
]

Actually, expression in JSON contains $ to represent one of the dictionary objects value from JSON. Wherein framework internally treats it as a local variable.

这些表达式也可能有不同的组合。除了“必需”参数外,可能还有几个表达式。我需要为 UI 中值的任何变化运行所有相关表达式。

编辑

let expression = "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"
let dict = ["firstName": "Amir", "lastName": ""]
let e = try! Expression(string: expression)
let result = try! Evaluator.default.evaluate(e, substitutions: dict)

虽然它应该从 JSON 中解析出一个正确的值,但我已经硬编码了替换字符串以至少取得突破。此处,替换期望 String & Double & 给出错误,如 “无法将类型 [String: String] 的值转换为预期的 arg 类型替换 (Dcitionary)。有什么方法可以传递 String: 字符串替换吗?

最佳答案

DDMathParser 不是为进行字符串评估而构建的。 技术上可以让它发挥作用,但这有点超出了框架的范围。

对于这种情况,我认为您最好使用 NSPredicate,它确实允许字符串比较和变量替换。

关于ios - DDMathParser:解析包含 $ 的动态公式以引用字典中的对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com