如何在Xcode中删除“未使用的变量”警告?

时间:2021-12-15 23:50:57

I understand exactly why unused variable warnings occur. I don't want to suppress them in general, because they are incredibly useful in most cases. However, consider the following (contrived) code.

我很清楚为什么会出现未使用的变量警告。一般来说,我不想压制它们,因为它们在大多数情况下都非常有用。但是,请考虑以下(人为设计的)代码。

NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);

Xcode reports that saved is an unused variable, when of course it isn't. I suspect this is because NSAssert1 is a macro. The NS_BLOCK_ASSERTIONS macro is not defined, so Objective C assertions are definitely enabled.

Xcode报告保存的是一个未使用的变量,当然它不是。我怀疑这是因为NSAssert1是一个宏。ns_block_断言宏没有定义,所以Objective - C断言肯定是启用的。

While it doesn't hurt anything, I find it untidy and annoying, and I want to suppress it, but I'm not sure how to do so. Assigning the variable to itself gets rid of the compiler warning, but I'd rather do it the "right" way if such a thing exists.

虽然它不会伤害任何东西,但我觉得它很不整洁,很烦人,我想要压制它,但我不知道该怎么做。将变量赋值给它本身就可以去掉编译器警告,但如果存在这样的情况,我宁愿做“正确”的方法。

10 个解决方案

#1


96  

I'm unsure if it's still supported in the new LLVM compiler, but GCC has an "unused" attribute you can use to suppress that warning:

我不确定它是否仍然在新的LLVM编译器中得到支持,但是GCC有一个“未使用的”属性,您可以使用它来抑制这个警告:

BOOL saved __attribute__((unused)) = [moc save:&error];

Alternatively (in case LLVM doesn't support the above), you could split the variable declaration into a separate line, guaranteeing that the variable would be "used" whether the macro expands or not:

或者(如果LLVM不支持上述),则可以将变量声明拆分为单独的行,以保证该变量将被“使用”,无论宏是否展开:

BOOL saved = NO;
saved = [moc save:&error];

#2


93  

Using Xcode 4.3.2 and found out that this seems to work (less writing)

使用Xcode 4.3.2,发现这似乎起作用(更少的文字)

BOOL saved __unused;

#3


32  

In Xcode you can set the warnings for "Unused Variables." Go to "Build Settings" for the target and filter with the word "unused"

在Xcode中,可以为“未使用的变量”设置警告。使用“未使用”这个词来为目标和过滤器“构建设置”

Here is a screenshot: 如何在Xcode中删除“未使用的变量”警告?

这里是一个截图:

I suggest you only change it for Debug. That way you don't miss anything in your release version.

我建议您只将它更改为Debug。这样你就不会错过发布版本中的任何东西。

#4


21  

NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);
#pragma unused(saved)

Try like this. It is working for me. It will work for you, too.

这样的尝试。它在为我工作。它也会为你工作。

#5


14  

The only simple and portable way to mark variable as used is… to use it.

用于标记变量的唯一简单和可移植的方法是…使用它。

BOOL saved = ...;
(void)saved; // now used

You may be happy with already described compiler-specific extensions, though.

不过,您可能对已经描述的特定于编译器的扩展感到满意。

#6


7  

try with: __unused attribute. Works in Xcode 5

试一试:__unused属性。在Xcode工作5

#7


4  

You can set "No" LLVM compliler 2.0 warning on "Release" 如何在Xcode中删除“未使用的变量”警告?

您可以在“Release”中设置“不”LLVM依从2.0警告

#8


3  

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

SOURCE

#9


3  

This is the way you do it in C and therefore also Objective-C.

这是在C中做的,因此也是Objective-C。

Even though you do not have warning enabled, it's always a good idea to mark the return value as explicitly ignored. It also goes to shows other developers, that you have not just forgotten about the return value – you have indeed explicitly chosen to ignore it.

尽管您没有启用警告,但是将返回值标记为显式忽略总是一个好主意。它还向其他开发人员展示,您不仅忘记了返回值——您确实明确地选择忽略它。

(void)[moc save:&error];

EDIT: Compilers ignore casts to void, so it should not affect performance – it's just a nice clean human annotation.

编辑:编译器忽略强制转换,所以它不应该影响性能——它只是一个干净的人工注释。

#10


1  

Make it take up two lines. Separate the declaration and default value

让它占据两条线。分离声明和默认值。

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;

#1


96  

I'm unsure if it's still supported in the new LLVM compiler, but GCC has an "unused" attribute you can use to suppress that warning:

我不确定它是否仍然在新的LLVM编译器中得到支持,但是GCC有一个“未使用的”属性,您可以使用它来抑制这个警告:

BOOL saved __attribute__((unused)) = [moc save:&error];

Alternatively (in case LLVM doesn't support the above), you could split the variable declaration into a separate line, guaranteeing that the variable would be "used" whether the macro expands or not:

或者(如果LLVM不支持上述),则可以将变量声明拆分为单独的行,以保证该变量将被“使用”,无论宏是否展开:

BOOL saved = NO;
saved = [moc save:&error];

#2


93  

Using Xcode 4.3.2 and found out that this seems to work (less writing)

使用Xcode 4.3.2,发现这似乎起作用(更少的文字)

BOOL saved __unused;

#3


32  

In Xcode you can set the warnings for "Unused Variables." Go to "Build Settings" for the target and filter with the word "unused"

在Xcode中,可以为“未使用的变量”设置警告。使用“未使用”这个词来为目标和过滤器“构建设置”

Here is a screenshot: 如何在Xcode中删除“未使用的变量”警告?

这里是一个截图:

I suggest you only change it for Debug. That way you don't miss anything in your release version.

我建议您只将它更改为Debug。这样你就不会错过发布版本中的任何东西。

#4


21  

NSError *error = nil;
BOOL saved = [moc save:&error];
NSAssert1(saved, @"Dude!!1! %@!!!", error);
#pragma unused(saved)

Try like this. It is working for me. It will work for you, too.

这样的尝试。它在为我工作。它也会为你工作。

#5


14  

The only simple and portable way to mark variable as used is… to use it.

用于标记变量的唯一简单和可移植的方法是…使用它。

BOOL saved = ...;
(void)saved; // now used

You may be happy with already described compiler-specific extensions, though.

不过,您可能对已经描述的特定于编译器的扩展感到满意。

#6


7  

try with: __unused attribute. Works in Xcode 5

试一试:__unused属性。在Xcode工作5

#7


4  

You can set "No" LLVM compliler 2.0 warning on "Release" 如何在Xcode中删除“未使用的变量”警告?

您可以在“Release”中设置“不”LLVM依从2.0警告

#8


3  

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

SOURCE

#9


3  

This is the way you do it in C and therefore also Objective-C.

这是在C中做的,因此也是Objective-C。

Even though you do not have warning enabled, it's always a good idea to mark the return value as explicitly ignored. It also goes to shows other developers, that you have not just forgotten about the return value – you have indeed explicitly chosen to ignore it.

尽管您没有启用警告,但是将返回值标记为显式忽略总是一个好主意。它还向其他开发人员展示,您不仅忘记了返回值——您确实明确地选择忽略它。

(void)[moc save:&error];

EDIT: Compilers ignore casts to void, so it should not affect performance – it's just a nice clean human annotation.

编辑:编译器忽略强制转换,所以它不应该影响性能——它只是一个干净的人工注释。

#10


1  

Make it take up two lines. Separate the declaration and default value

让它占据两条线。分离声明和默认值。

BOOL enabled = NO;

// ...

BOOL enabled;

enabled = NO;