わかりました、私はこれに苦労しています:私はすでに引数なしでカスタム関数を次のように動作させることができました:
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/#コメント-549856
最後に彼は、Objective-C でそれを行う方法を説明しますが、Swift では行いません。それを行う方法はありますか?ところで、これは私の電卓アプリ用です。