Ste*_*fan 7 compiler-warnings swift
我有一个Swift函数做这样的事情:
func f() -> Int {
switch (__WORDSIZE) {
case 32: return 1
case 64: return 2
default: return 0
}
}
Run Code Online (Sandbox Code Playgroud)
因为__WORDSIZE是常量,所以编译器总是在开关体中给出至少一个警告.实际标记了哪些行取决于我正在构建的目标(例如iPhone 5对6;有趣的是iPhone 5给出了64位情况的警告,而iPhone 6给出了32位和默认的两个警告).
我发现,雨燕的等价物#pragma是// MARK:,所以我尝试
// 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
Run Code Online (Sandbox Code Playgroud)
但警告仍然存在,MARKs似乎没有效果.
作为一种解决方法,我现在有这样的事情:
#if arch(arm) || arch(i386)
return 1
#else
#if arch(arm64) || arch(x86_64)
return 2
#else
return 0
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
- 但当然这不一样.任何提示......?
目前(Xcode 7.1),似乎无法在Swift中抑制特定警告(参见例如如何在swift中静音警告).
在您的特殊情况下,您可以通过计算单词中的字节数来欺骗编译器:
func f() -> Int {
switch (__WORDSIZE / CHAR_BIT) { // Or: switch (sizeof(Int.self))
case 4: return 1
case 8: return 2
default: return 0
}
}
Run Code Online (Sandbox Code Playgroud)
这在32位和64位体系结构上编译时没有警告.