避免“表达式结果未使用”块中的警告

发布于 2024-12-28 08:17:33 字数 310 浏览 1 评论 0原文

以下代码在块中的赋值操作上返回表达式未使用警告。该代码不太实用,但排除部分中有更多代码,并且这些代码必须在特定队列上运行。

__block NSNumber *pageId=nil;
dispatch_sync(_myDispatchQueue, ^{
    int val;
    //... code generates an int and puts it in val
    pageId = [NSNumber numberWithInt:val];
}];
//pageId used below

我该如何摆脱这个错误?

The following code is returning an expression unused warning on the assignment operation in the block. The code isn't very practical, but there is a lot more code in the excluded section and that code has to run on a particular queue.

__block NSNumber *pageId=nil;
dispatch_sync(_myDispatchQueue, ^{
    int val;
    //... code generates an int and puts it in val
    pageId = [NSNumber numberWithInt:val];
}];
//pageId used below

How do I get rid of this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2025-01-04 08:17:33
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
 pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
 pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop
扶醉桌前 2025-01-04 08:17:33

我的实验结果

笔记我从 Intrubidus 那里得到了这个,但我想要更多信息,所以在实验之后我在这里记录了我的发现,供下一个人使用。

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop

仅适用于忽略和弹出之间的区域。 “-Wunused-value”不会抑制未使用的变量。

这是抑制未使用的变量的方法:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
int i = 0;
#pragma clang diagnostic pop

另外,没有推送和弹出,如图所示:

#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];

该文件中#pragma之后的任何位置都会忽略警告类型。这似乎仅适用于有问题的文件。

希望您觉得这很有用,
    -大通

My Experimental Findings

Note I got this from Intrubidus, but I wanted additional information so after experimenting I recorded my findings here for the next guy.

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop

Only applies to the area between the ignore and the pop. "-Wunused-value" does not suppress unused variables.

This is how you would suppress unused variables:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
int i = 0;
#pragma clang diagnostic pop

Also, without the push and pop, as shown:

#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];

The type of warning was ignored anywhere in that file after the #pragma. This seems to only apply to the file in question.

Hope you found this useful,
    - Chase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文