Alright guys, I'm struggling with this: I already managed to make custom functions without arguments work like this:
NSExpression(format: "function(1+1, 'myCustomFunction'").expressionValueWithObject(nil, context: nil) as! Double
But I have no idea of how to implement a custom function with arguments, I already tried passing the following String to NSExpression without success:
"function(1+1, 'myCustomFunctionWithArgument(42)')" // 42 as the argument
where the custom function looks like:
public extension NSNumber {
func myCustomFunctionWithArgument(x: Double) -> NSNumber
//...
}
However I get the following error:
-[__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
I already searched everywhere, and I only find Objective-C answers, like on this tutorial: https://spin.atomicobject.com/2015/03/24/evaluate-string-expressions-ios-objective-c-swift/#comment-549856
In the end he explains how to do it on Objective-C, but not in Swift. Is there any way to do it? Btw this is for my Calculator app.
I got the code below to work in an Xcode 7.2.1 playground. The error you received had to do with the selector, and the main thing to keep in mind for selectors is that you need a colon (:) after the name of the function in this case to signify that the function has a single parameter.
import Foundation
public extension NSNumber {
func myCustomFunctionWithArgument(x: NSNumber) -> NSNumber {
return self.doubleValue + x.doubleValue
}
}
let stringExpression = "function(1+1, 'myCustomFunctionWithArgument:', 42)"
let expression = NSExpression(format: stringExpression)
let result = expression.expressionValueWithObject(nil, context: nil) as! NSNumber
print(result.doubleValue)