提交基于 swift 的 cocoapod 时,在检查设备是否为模拟器时出现此错误:'return' 之后的代码将永远不会执行

When submitting a swift based cocoapod, I get this error when checking if the device is a simulator: code after 'return' will never be executed

我正在尝试提交一个用 Swift 编写的 cocoapod,其中包含以下代码方法,旨在防止在针对模拟器时执行特定代码:

func isDevice() -> Bool {
    #if (arch(i386) || arch(x86_64)) && os(iOS)
        return false
    #endif

    return true
}

虽然 XCode 认为这是可以接受的,并且我可以使用 --allow-warnings 标志抑制来自 pod lib lint 的警告,但尝试提交 pod 仍然会失败。

此代码产生警告 warning: code after 'return' will never be executed

我犯的错误是,虽然 source of this answer 是可靠的,但我没有正确执行条件检查。

以下是避免 Cocoapod 验证问题的正确方法:

func isDevice() -> Bool {
        #if (arch(i386) || arch(x86_64)) && os(iOS)
            return false
        #else
            return true
        #endif
    }

通过将检查放在 #if...#endif 内,我能够避免警告。