Swift中可选的成本

时间:2017-08-11 17:57:24

标签: swift performance optional

我想知道是否存在与Swift中的选项相关的任何性能成本,例如,如果我们必须重复使用选项。请考虑以下两个代码片段 第一种方法

    var f: Foo?
    // foo returns an optional
    if let anotherF = foo() {
        f = anotherF
    }
    else {
        f = Foo()
    }
    f?.doSomething()
    f?.doSomethingElse()
    f?.doAnotherThing()
    // and so on

第二种方法

    var f = Foo()
    if let anotherF = foo() {
        f = anotherF
    }
    f.doSomething()
    f.doAnotherThing()
    f.doAnotherThing()
    // and so on

与第一种方法相比,第二种方法是否有任何性能优势

0 个答案:

没有答案