如何为AppIntent的Double类型@Parameter定义单侧无上限包含范围?
在AppIntent中为Double类型@Parameter定义单侧开放范围(仅正数)
问题描述
我需要给AppIntent的Double类型@Parameter设定仅包含正数的范围,类似Swift中的0...单侧开放范围。已知@Parameter针对int/double变量支持inclusiveRange类型别名,但不清楚如何定义这种只有下限、无上限的范围。目前我临时用一个极大值作为upperBound,但想找更规范的实现方式。
当前临时代码
@Parameter(title: "Reading", description: "The numeric value of the reading.", controlStyle: .field, inclusiveRange: (lowerBound: 0, upperBound: 100000000), requestValueDialog: IntentDialog("What is the numeric value of the reading?")) var value: Double
规范解决方案
可以使用Double.greatestFiniteMagnitude作为upperBound,它是Double类型能表示的最大有限值,比手动指定大数更符合标准:
@Parameter(title: "Reading", description: "The numeric value of the reading.", controlStyle: .field, inclusiveRange: (lowerBound: 0, upperBound: Double.greatestFiniteMagnitude), requestValueDialog: IntentDialog("What is the numeric value of the reading?")) var value: Double
如果需要严格排除0,确保输入是正数值,可以将lowerBound设为Double.leastNonzeroMagnitude,这是Double能表示的最小非零正数,避免用户输入0的情况。
内容的提问来源于stack exchange,提问作者alexkaessner
相关产品推荐
相关产品推荐

