7

如何仅在一个地方禁用一个警告?

我有一个暂时不用的变量。Xcode 向我显示有关“未使用变量”的警告。我想禁用警告,但仅针对此变量,并非所有此类警告。

是否可以不设置/获取此变量的值?

4

4 回答 4

10

这很简单:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
    NSUInteger abc; /// Your unused variable
#pragma clang diagnostic pop

但是保留未使用的变量是多余的,而且通常是个坏主意。应该删除未使用的代码。如果您使用 git,所有更改仍在您的存储库中,如果您发现此变量是必要的,您可以恢复您的代码。

于 2014-02-19T07:50:00.187 回答
7

来自GCC / 指定变量的属性(Clang 也可以理解):

int x __attribute__ ((unused));

或者

int y __attribute__((unused)) = initialValue ;
于 2014-02-19T07:42:53.473 回答
3
__unused int theInt = 0;
// there will be no warning, but you are still able to use `theInt` in the future
于 2016-02-10T23:48:38.770 回答
0

最短击键答案,更改:

int x; // this variable temporarily unused

至:

// int x; // this variable temporarily unused

警告就会消失。此外,当您再次需要该变量时,您不能忘记将其删除,您可以使用任何保留声明变量的方法来执行此操作。

如果您希望删除更加明显,但希望保留您不能忘记删除它的属性,请尝试:

#if 0
int x; // this variable temporarily unused
#endif

高温高压

于 2014-02-19T09:19:37.767 回答