Result of call to [myFunction] is unused

后端 未结 2 534
无人共我
无人共我 2020-12-03 11:23

In Obj-C, a common practice was to use convenience functions to perform common operations, like configuring auto layout for views:

func makeConstraint(withAn         


        
相关标签:
2条回答
  • 2020-12-03 11:41

    This is behaviour that has been introduced in Swift 3. Instead of having to explicitly annotate functions with @warn_unused_result in order to tell the compiler that the result should be used by the caller, this is now the default behaviour.

    You can use the @discardableResult attribute on your function in order to inform the compiler that the return value doesn't have to be 'consumed' by the caller.

    @discardableResult
    func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint {
    
       ... // do things that have side effects
    
       return NSLayoutConstraint()
    }
    

    view1.makeConstraint(view2) // No warning
    
    let constraint = view1.makeConstraint(view2) // Works as expected
    

    You can read about this change in more detail on the evolution proposal.

    0 讨论(0)
  • 2020-12-03 11:41

    You can try to turn the warnings off from the Build Settings of your project. I like the question, good point. Done some research and found this. https://stackoverflow.com/a/34932152/5615274

    Did not test it yet.

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