Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中抑制特定警告_Swift_Compiler Warnings - Fatal编程技术网

如何在Swift中抑制特定警告

如何在Swift中抑制特定警告,swift,compiler-warnings,Swift,Compiler Warnings,我有一个Swift函数,可以执行以下操作: func f() -> Int { switch (__WORDSIZE) { case 32: return 1 case 64: return 2 default: return 0 } } #if arch(arm) || arch(i386) return 1 #else #if arch(arm64) || arch(x86_64) ret

我有一个Swift函数,可以执行以下操作:

func f() -> Int {
    switch (__WORDSIZE) {
        case 32: return 1
        case 64: return 2
        default: return 0
    }
}
#if arch(arm) || arch(i386)
    return 1
#else
    #if arch(arm64) || arch(x86_64)
        return 2
    #else
        return 0
    #endif
#endif
因为
\uuuuWordSize
是一个常量,编译器总是在开关体中至少给出一个警告。实际标记哪些行取决于我构建的目标(例如,iPhone 5和6;有趣的是,iPhone 5对64位的情况给出了警告,而iPhone 6对32位和默认情况给出了两个警告)

我发现
#pragma
的Swift等价物是
//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
但是警告仍然存在,
标记
s似乎没有效果

作为一种解决方法,我现在有如下内容:

func f() -> Int {
    switch (__WORDSIZE) {
        case 32: return 1
        case 64: return 2
        default: return 0
    }
}
#if arch(arm) || arch(i386)
    return 1
#else
    #if arch(arm64) || arch(x86_64)
        return 2
    #else
        return 0
    #endif
#endif
–但这当然不一样。任何提示…?

目前(Xcode 7.1),似乎无法抑制Swift中的特定警告(参见示例)

在您的特殊情况下,您可以通过以下方式愚弄编译器 计算字中的字节数:

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

这在32位和64位体系结构上编译时都没有警告。

目前,您无法在Swift代码中抑制特定警告(请参见示例)。作为一种解决方法,您可以改为打开
sizeof(Int.self)
(4或8)了解您为什么需要此函数会很有趣。谢谢,您的解决方案很有效。关于代码,请参见这里:-jstn的答案。我以前没有看到过,但是这里有一个(非常类似的)解决方案,它不需要切换字号。