Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress Swift compiler warning

I'm using the Nimble assertion framework for unit testing in Swift (Xcode 6.3 beta). It works fine, but the compiler gives a warning for one of the lines in the Nimble source code:

public func expect<T>(expression: () -> T?, file: String = __FILE__, line: UInt = __LINE__) -> Expectation<T> {
    return Expectation(
        expression: Expression(
            expression: expression,
            location: SourceLocation(file: file, line: line),
            isClosure: true))
}

The warning is for the first line:

Closure parameter prior to parameters with default arguments will not be treated as a trailing closure

It's not a very serious issue, but I'd like to keep the number of compiler warnings low (zero) in my projects. Is there a way to remove this warning?

like image 408
Bedford Avatar asked Mar 15 '15 15:03

Bedford


People also ask

How do I supress a warning in Xcode?

If you want to disable all warnings, you can pass the -suppress-warnings flag (or turn on "Suppress Warnings" under "Swift Compiler - Warning Policies" in Xcode build settings).

How do I turn off errors in Xcode?

Select your project and select your target and show Build Phases . Search the name of the file in which you want to hide, and you should see it listed in the Compile Sources phase. Double-click in the Compiler Flags column for that file and enter -w to turn off all warnings for that file.


1 Answers

You can avoid the warning if the method signature will look like:

public func expect<T>(expression: (() -> T?), file: String = __FILE__, line: UInt = __LINE__) -> Expectation<T> 

added extra parenthesis around the first argument, tested with Swift 2.0 and Xcode 7.1

Another way of fixing it is to have all attributes with default value before the closure attribute as trailing closure is a quite convenient thing to have

like image 179
Julian Avatar answered Oct 09 '22 11:10

Julian