![](/img/trans.png)
[英]How can I convert data into types like Doubles, Ints and Strings in Swift?
[英]Can I force NSExpression and expressionValue to assume Doubles instead of Ints somehow?
我正在嘗試用字符串做數學。
當我使用NSExpression將字符串轉換為數學問題,然后使用expressionValue獲取結果時,Swift假設我想要一個整數。 考慮這兩個Playground示例:
let currentCalculation = "10 / 6"
let currentExpression = NSExpression(format: currentCalculation)
print(currentExpression) // 10 / 6
if let result = currentExpression.expressionValue(with: nil, context: nil) as? Double {
print(result) // 1
}
let anotherCalculation = "10.0 / 6.0"
let anotherExpression = NSExpression(format: anotherCalculation)
print(anotherExpression) // 10 / 6
if let result = anotherExpression.expressionValue(with: nil, context: nil) as? Double {
print(result) // 1.666666667
}
我應該怎么做才能讓我總是得到一個Double? 我不想提前解析字符串。
非常有趣的是,第二個示例將“anotherExpression”轉換為Integers,但仍然返回Double作為結果。
您可能最好使用第三方表達式解析器/評估程序,例如DDMathParser 。 NSExpression
非常有限,沒有強制浮點評估的選項。
如果你想(或必須)堅持NSExpression
:這是一個可能的解決方案(遞歸地)用它們的浮點值替換表達式中的所有常量值:
extension NSExpression {
func toFloatingPoint() -> NSExpression {
switch expressionType {
case .constantValue:
if let value = constantValue as? NSNumber {
return NSExpression(forConstantValue: NSNumber(value: value.doubleValue))
}
case .function:
let newArgs = arguments.map { $0.map { $0.toFloatingPoint() } }
return NSExpression(forFunction: operand, selectorName: function, arguments: newArgs)
case .conditional:
return NSExpression(forConditional: predicate, trueExpression: self.true.toFloatingPoint(), falseExpression: self.false.toFloatingPoint())
case .unionSet:
return NSExpression(forUnionSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .intersectSet:
return NSExpression(forIntersectSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .minusSet:
return NSExpression(forMinusSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .subquery:
if let subQuery = collection as? NSExpression {
return NSExpression(forSubquery: subQuery.toFloatingPoint(), usingIteratorVariable: variable, predicate: predicate)
}
case .aggregate:
if let subExpressions = collection as? [NSExpression] {
return NSExpression(forAggregate: subExpressions.map { $0.toFloatingPoint() })
}
case .anyKey:
fatalError("anyKey not yet implemented")
case .block:
fatalError("block not yet implemented")
case .evaluatedObject, .variable, .keyPath:
break // Nothing to do here
}
return self
}
}
例:
let expression = NSExpression(format: "10/6+3/4")
if let result = expression.toFloatingPoint().expressionValue(with: nil, context: nil) as? Double {
print("result:", result) // 2.41666666666667
}
這適用於使用算術運算符和函數的“簡單”表達式以及一些“高級”表達式類型(聯合,交集,......)。 如有必要,可以添加剩余的轉化次數。
這是Martin R的一個很好的答案,有兩個重要的變化:
count({1,2,3,4,5}) / count({1,2})
這樣的表達式,其中除法的參數不是常量值。 碼:
import Foundation
extension NSExpression {
func toFloatingPointDivision() -> NSExpression {
switch expressionType {
case .function where function == "divide:by:":
guard let args = arguments else { break }
let newArgs = args.map({ arg -> NSExpression in
if arg.expressionType == .constantValue {
if let value = arg.constantValue as? Double {
return NSExpression(forConstantValue: value)
} else {
return arg
}
} else {
return NSExpression(block: { (object, arguments, context) in
// NB: The type of `+[NSExpression expressionForBlock:arguments]` is incorrect.
// It claims the arguments is an array of NSExpressions, but it's not, it's
// actually an array of the evaluated values. We can work around this by going
// through NSArray.
guard let arg = (arguments as NSArray).firstObject else { return NSNull() }
return (arg as? Double) ?? arg
}, arguments: [arg.toFloatingPointDivision()])
}
})
return NSExpression(forFunction: operand, selectorName: function, arguments: newArgs)
case .function:
guard let args = arguments else { break }
let newArgs = args.map({ $0.toFloatingPointDivision() })
return NSExpression(forFunction: operand, selectorName: function, arguments: newArgs)
case .conditional:
return NSExpression(forConditional: predicate,
trueExpression: self.true.toFloatingPointDivision(),
falseExpression: self.false.toFloatingPointDivision())
case .unionSet:
return NSExpression(forUnionSet: left.toFloatingPointDivision(), with: right.toFloatingPointDivision())
case .intersectSet:
return NSExpression(forIntersectSet: left.toFloatingPointDivision(), with: right.toFloatingPointDivision())
case .minusSet:
return NSExpression(forMinusSet: left.toFloatingPointDivision(), with: right.toFloatingPointDivision())
case .subquery:
if let subQuery = collection as? NSExpression {
return NSExpression(forSubquery: subQuery.toFloatingPointDivision(), usingIteratorVariable: variable, predicate: predicate)
}
case .aggregate:
if let subExpressions = collection as? [NSExpression] {
return NSExpression(forAggregate: subExpressions.map({ $0.toFloatingPointDivision() }))
}
case .block:
guard let args = arguments else { break }
let newArgs = args.map({ $0.toFloatingPointDivision() })
return NSExpression(block: expressionBlock, arguments: newArgs)
case .constantValue, .anyKey:
break // Nothing to do here
case .evaluatedObject, .variable, .keyPath:
// FIXME: These should probably be wrapped in blocks like the one
// used in the `.function` case.
break
}
return self
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.