iOS Xcode Swift autocomplete broken?

前端 未结 1 1433
忘了有多久
忘了有多久 2020-12-10 19:05

I haven\'t used Swift that much but coming from Objective C, there\'s a few things about Swift that\'s a PITA to get my head around.

In iOS programming, we have

相关标签:
1条回答
  • 2020-12-10 19:35

    From "Calling Methods Through Optional Chaining":

    Any attempt to set a property through optional chaining returns a value of type Void?, which enables you to compare against nil to see if the property was set successfully ...

    Therefore the type of the expression

    self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
    

    is Void? (optional Void). And if a closure consists only of a single expression, then this expression is automatically taken as the return value. The error message is quite misleading, but it comes from the fact that Void? is different from Void.

    Adding an explicit return statement solves the problem:

    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
        return
    })
    

    Update: Adding an explicit return statement it not necessary anymore with Swift 1.2 (Xcode 6.3). From the beta release notes:

    Unannotated single-expression closures with non-Void return types can now be used in Void contexts.

    0 讨论(0)
提交回复
热议问题