I have a Swift function doing something like this:
func f() -> Int {
switch (__WORDSIZE) {
case 32: return 1
case 64: return 2
default: return 0
}
}
Because __WORDSIZE
is a constant, the compiler always gives at least one warning in the switch body. Which lines are actually marked depends on the target I am building for (e.g. iPhone 5 vs. 6; interestingly iPhone 5 gives a warning for the 64-bit case whereas iPhone 6 gives two warnings for 32-bit and default).
I found out that the Swift equivalent for #pragma
is // MARK:
, so I tried
// MARK: clang diagnostic push
// MARK: clang diagnostic ignored "-Wall"
func f() -> Int {
switch (__WORDSIZE) {
case 32: return 1
case 64: return 2
default: return 0
}
}
// MARK: clang diagnostic pop
but the warnings remain, the MARK
s seem to have no effect.
As a workaround, I now have something like this:
#if arch(arm) || arch(i386)
return 1
#else
#if arch(arm64) || arch(x86_64)
return 2
#else
return 0
#endif
#endif
– but of course this is not the same. Any hints…?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With