NSExpression\NSNumber extension changed in Swift 3.0

我与影子孤独终老i 提交于 2019-12-12 03:34:23

问题


have some extension for NSNumber:

extension NSNumber {
    func factorialF() -> NSNumber {
        return tgamma(self.doubleValue + 1)
    }
}

then i use this extension in my calculation

var stringFunction: String = "FUNCTION(10,'factorialF')"
var expn: NSExpression = NSExpression(format: stringFunction)
var result = expn.expressionValueWithObject(with: nil, context: nil)

in Xcode 7.3.1 and Swift 2.2 all works but in Xcode 8 and Swift 3.0 i have some error in my extension NSNumber "No 'tgamma' candidates produce the expected contextual result type 'NSNumber'"

i was fixed this error but get new

extension NSNumber {
    func factorialF() -> Double {
        return tgamma(self.doubleValue + 1)
    }
}
var stringFunction: String = "FUNCTION(10,'factorialF')"
var expn: NSExpression = NSExpression(format: stringFunction)
var result = expn.expressionValue(with: nil, context: nil)

i got new error here

var result = expn.expressionValue(with: nil, context: nil)

error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

how correct fix the first error in extension NSNumber?


回答1:


fixed by

extension NSNumber {
    func factorialF() -> NSNumber {
        return tgamma(self.doubleValue + 1) as NSNumber
    }
}

and all works in Swift 3.0



来源:https://stackoverflow.com/questions/39493898/nsexpression-nsnumber-extension-changed-in-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!