How to Suppress Warnings in Swift 3

How to suppress warnings in Swift 3?

EDIT: below instruction is for "deprecated declarations" warning. If you want to suppress different warnings then you should use flag relevant for the warning. Most of you probably use Clang, and it's warning flags can be found here. So if you want to suppress for example -Wunused-argument you will write it with "no": -Wnounused-argument.

If you want to disable compiler warnings then go to Project -> Target -> Build Settings and add flag with no prefix to other warning flags:

for all files

If you want to disable warnings for separate file:
Go to Project and pick relevant Target -> Build Phases -> Compile Sources and flag separate file:

for one file

How to silence a warning in Swift?

As of 2021, Xcode 13.0, the consensus is that there is no way to achieve that.

I'll update/edit this answer if Apple add the feature.

Put it in your wish list for WWDC 2022 !

How to suppress a specific warning in Swift

At present (Xcode 7.1), there seems to be no way of suppressing a specific warning in Swift (see e.g. How to silence a warning in swift).

In your special case, you can fool the compiler by
computing the number of bytes in a word:

func f() -> Int {
switch (__WORDSIZE / CHAR_BIT) { // Or: switch (sizeof(Int.self))
case 4: return 1
case 8: return 2
default: return 0
}
}

This compiles without warnings on both 32-bit and 64-bit architectures.

In Xcode, how to suppress all warnings in specific source files?

Select your target and show Build Phases. Then enter the name of the file in the search box, 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.

Xcode: suppressing all warnings for all external libraries

Quick Fix

I see you are using cocoapods.
Cocoapods rewrite your configs each time you run pod install. So, you need to add this line in your podfile to ignore all warnings or warnings for a specific pod:

# example to ignore all warnings from all pods
inhibit_all_warnings!

# example to ignore warnings from a specific pod
pod 'Alamofire', :inhibit_warnings => true

NOTE: Sometimes it is good to see your warnings and your pod's warnings too, so you could prevent issues in the future.

Removing warnings

Add @discardableResult to your function.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
neglectable()
}

@discardableResult func neglectable() -> String {
return ""
}
}

Suppress Swift compiler warning

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

How to suppress compiler warning

With an else statement:

guard let isNotNil = anOptional
else {
#if DEBUG
fatalError()
#else
return false
#endif

}

You can also go to the project or the target build settings and disable the warning for unreachable code.



Related Topics



Leave a reply



Submit