【发布时间】:2016-06-30 03:28:39
【问题描述】:
好吧,伙计们,我正在为此苦苦挣扎:我已经设法让没有参数的自定义函数像这样工作:
NSExpression(format: "function(1+1, 'myCustomFunction'").expressionValueWithObject(nil, context: nil) as! Double
但我不知道如何使用参数实现自定义函数,我已经尝试将以下字符串传递给 NSExpression,但没有成功:
"function(1+1, 'myCustomFunctionWithArgument(42)')" // 42 as the argument
自定义函数的样子:
public extension NSNumber {
func myCustomFunctionWithArgument(x: Double) -> NSNumber
//...
}
但是我得到以下错误:
-[__NSCFNumber myCustomFunctionWithArgument(42)]: unrecognized selector sent to instance 0xb000000000000045
2016-03-14 18:23:00.755 MyApp[3517:263390] *** Terminating app due to uncaught exception
我已经到处搜索了,我只找到了 Objective-C 的答案,就像在这个教程中一样: https://spin.atomicobject.com/2015/03/24/evaluate-string-expressions-ios-objective-c-swift/#comment-549856
最后他解释了如何在 Objective-C 上做到这一点,而不是在 Swift 中。有什么办法吗?顺便说一句,这是我的计算器应用程序。
【问题讨论】:
标签: swift calculator nsnumber nsexpression